Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
sir,
i want to save watermarked image to the database.By using my code the image is to be watermarked and it doesn't save to the database.The datatype is used for image as image type.Here is my code...
C#
 Bitmap original_image = new Bitmap(FileUpload1.FileContent);
 string sWaterMark = "reshu";//String to be watermarked
 int fontsize = ((original_image.Width * 2) / (sWaterMark.Length * 3));
 int x = original_image.Width / 2;
 int y = original_image.Height * 9 / 20;

 StringFormat drawFormat = new StringFormat();
 drawFormat.Alignment = StringAlignment.Center;
 drawFormat.FormatFlags = StringFormatFlags.NoWrap;

 //drawing string on Image
 Graphics graphic = Graphics.FromImage(original_image);
 graphic.DrawString(sWaterMark, new Font("Verdana", fontsize, FontStyle.Bold), new SolidBrush(Color.FromArgb(80, 255, 255, 255)), x, y, drawFormat);

 original_image.Save(MapPath("~/" +"/" + FileUpload1.PostedFile.FileName));
 if (original_image != null) original_image.Dispose();
 if (graphic != null) graphic.Dispose();

 string image_name = FileUpload1.PostedFile.FileName;


 con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\website\MapExtractor\App_Data\MapDB.mdf;Integrated Security=True;User Instance=True");

 con.Open();
 cmd = new SqlCommand("insert into Table1(name,image) values('"+TextBox3.Text+"','"+image_name+"')", con);

// cmd.Parameters.AddWithValue("@name", TextBox3.Text);
 // int img = FileUpload1.PostedFile.ContentLength;

 //byte[] msdata = new byte[img];

 //FileUpload1.PostedFile.InputStream.Read(msdata, 0, img);

// cmd.Parameters.AddWithValue("@image", bmp);
 cmd.ExecuteNonQuery();

 Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('Image Added')", true);
 con.Close();


please check it my code and help me..
Posted
Updated 16-Oct-14 21:29pm
v2
Comments
Sinisa Hajnal 17-Oct-14 3:23am    
Are two slashes on purpose?
MapPath("~/" +"/"...)

Do you get an error? Or it saves the uploaded image without watermark? Did you verify (by saving the image to disk)....

Finally, after changing the image, nowhere in the code under that you should user Posted file stream. You should open your new image and use its stream.
Reshma CS 17-Oct-14 3:29am    
no error is occured.By using 2 slashes, to save current directory.
image is watermarked,but it doesn't save to database.i am verify the watermarked image.

1 solution

Ah. Oh dear...

Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

And there is your problem: what you save to the db is the name of the file rather than the file itself - so when you read it back, you have to then access the file to get at the image - which is complicated, because an IMAGE datatype is a binary array of data, and doesn't convert nicely to a string, so you can't easily use it to re-open teh file.

Either store the image data in the DB (Why do I get a "Parameter is not valid." exception when I read an image from my database?[^] explains how) or store the whole path to the file in a NVARCHAR column instead.

And BTW: don't store all your images in the root of your website: it makes it a lot harder for you to find things later. Use a folder structure instead, just as you would on your own HDD.
And a caution for you: since you store uploaded files, what happens when two users upload files with the same name? Oops....
 
Share this answer
 
Comments
Sinisa Hajnal 17-Oct-14 4:07am    
I agree with all of the above. I strongly suggest you make the changes OriginalGriff suggests.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900