如何将水印图像保存到数据库。 [英] how to save watermarked image to database.

查看:106
本文介绍了如何将水印图像保存到数据库。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生,

i希望将水印图像保存到数据库中。通过使用我的代码,图像将被加水印,并且不会保存到数据库。数据类型用于图像作为图像type.Here是我的代码...

位图original_image =  new 位图(FileUpload1.FileContent); 
string sWaterMark = reshu; // 要加水印的字符串
int fontsize =((ori​​ginal_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;

// 图片上的绘图字符串
Graphics graphic = Graphics .FromImage(original_image);
graphic.DrawString(sWaterMark, new 字体( 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表1(名称,图像)值(' + 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 (页面), 警告 alert('Image Added') true );
con.Close();





请检查我的代码并帮助我..

解决方案

阿。哦亲爱的...



不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。改为使用参数化查询。



并且存在问题:保存到数据库的是文件的名称而不是文件本身 - 所以当你阅读它时回来,你必须访问该文件才能获得图像 - 这很复杂,因为IMAGE数据类型是二进制数据数组,并且不能很好地转换为字符串,所以你不能轻易地使用它 - 打开文件。



将图像数据存储在数据库中(当我从数据库中读取图像时,为什么会出现参数无效。异常? [< a href =http://www.codeproject.com/Tips/465950/Why-do-I-get-a-Parameter-is-not-valid-exception-whtarget =_ blanktitle =New Window > ^ ]解释如何)或将整个路径存储在NVARCHAR列中的文件中。



和BTW:不要存储所有的您网站根目录中的图片:它让人眼前一亮rder让你以后找东西。使用文件夹结构,就像在你自己的硬盘上一样。

请注意:由于你存储了上传的文件,当两个用户上传同名文件时会发生什么?糟糕....

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...

 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..

解决方案

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....


这篇关于如何将水印图像保存到数据库。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆