水印添加到图像并保存数据库c#(winform) [英] Watermark add to image and save database c# (winform)
问题描述
如何将水印[text]添加到图像和存储到sql server的图像(C#winform)?
此致,
Karthikeyan,
Bangalore。
查看这些CP文章
使用GDI + for .NET创建带水印的照片 [ ^ ]
如何在C#ASP.NET中创建水印图像? [ ^ ]
以下代码将使用C#在图像上创建水印。
//使用System.Drawing使用NameSpace
;
//在活动中执行以下代码,你想要水印。
{
//创建Image的位图对象,这里我从文件控件FileTest中获取图像
位图bmp = new Bitmap(FileTest.PostedFile.InputStream);
Graphics canvas = Graphics.FromImage(bmp);
try
{
Bitmap bmpNew = new Bitmap(bmp.Width,bmp.Height);
canvas = Graphics.FromImage(bmpNew);
canvas.DrawImage(bmp,new Rectangle(0,0,
bmpNew.Width,bmpNew.Height),0,0,bmp.Width,bmp.Height,
GraphicsUnit.Pixel) ;
bmp = bmpNew;
}
catch(例外ee)//捕获异常
{
Response.Write(ee.Message);
}
//这里用文本替换文本,你也可以分配字体系列,颜色,文本位置等。
canvas.DrawString(Text,new Font( Verdana,14,
FontStyle.Bold),新的SolidBrush(Color.Beige),(bmp.Width / 2),
(bmp.Height / 2));
//保存或显示所需的图像。
bmp.Save(System.Web.HttpContext.Current.Server.MapPath(〜/
WaterMarkImages /)+
FileTest.PostedFile.FileName,
System.Drawing。 Imaging.ImageFormat.Jpeg);
}
用于插入图像到DB
参考以下链接
存储和检索来自SQL Server的图像使用标记过程和C#.net [ ^ ]
此代码将您的图像转换为适当的格式,您可以将图像保存到您的数据库。
private void btnBrowse_Click(object sender,EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog ();
openFileDialog1.InitialDirectory = @C:\;
openFileDialog1.Title =选择Ur图像文件;
openFileDialog1.Filter =所有文件(*。*)| *。*;
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
openFileDialog1.ReadOnlyChecked = true;
openFileDialog1.ShowReadOnly = true;
if(openFileDialog1.ShowDialog()== DialogResult.OK)
{
tbUpload.Text = openFileDialog1.SafeFileName;
string FilePath;
FilePath = openFileDialog1.FileName;
if(openFileDialog1.FileName!= null)
{
System.IO.FileStream fs = new System.IO.FileStream(FilePath,System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite);
System.IO.BinaryReader binaryReader = new System.IO.BinaryReader(fs);
long byteLength = new System.IO.FileInfo(FilePath).Length;
byte [] Attachmnt;
Attachmnt = binaryReader.ReadBytes((Int32)byteLength);
fs.Close();
fs.Dispose();
binaryReader.Close();
}
}
}
现在你可以通过storedprocedure或查询将{Attachmnt}的值存储到你的数据库中。(但你想要放置你的图像的列(Attachmnt)应该是iamge类型)
在这里,我通过浏览按钮通过openfiledialog解释它,你将选择你的图像。
但我不知道通过程序加水印。
Hi,
How to add watermark [text] to image and that image stored to sql server (C# winform)?
Regards,
Karthikeyan,
Bangalore.
Check these CP articles
Creating a Watermarked Photograph with GDI+ for .NET[^]
How to Create watermarked images in C# ASP.NET ?[^]
The below code will create a watermark on image using C#. //Use NameSpace using System.Drawing; //Execute below code at the event, where u wants Water Marking. { // Create a bitmap object of the Image, Here I am taking image from File Control "FileTest" Bitmap bmp = new Bitmap(FileTest.PostedFile.InputStream); Graphics canvas = Graphics.FromImage(bmp); try { Bitmap bmpNew = new Bitmap(bmp.Width, bmp.Height); canvas = Graphics.FromImage(bmpNew); canvas.DrawImage(bmp, new Rectangle(0, 0, bmpNew.Width, bmpNew.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel); bmp = bmpNew; } catch(Exception ee) // Catch exceptions { Response.Write(ee.Message); } // Here replace "Text" with your text and you also can assign Font Family, Color, Position Of Text etc. canvas.DrawString("Text", new Font("Verdana", 14, FontStyle.Bold), new SolidBrush(Color.Beige), (bmp.Width / 2), (bmp.Height / 2)); // Save or display the image where you want. bmp.Save(System.Web.HttpContext.Current.Server.MapPath("~/ WaterMarkImages/") + FileTest.PostedFile.FileName, System.Drawing.Imaging.ImageFormat.Jpeg); }
For insert image to DB
refer the following link
Storing and Retrieving Images from SQL Server Using Strored Procedures and C#.net[^]
This code will convert ur image to proper format by which u can save ur image to ur database.
private void btnBrowse_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.InitialDirectory = @"C:\"; openFileDialog1.Title = "Select Ur Image File"; openFileDialog1.Filter = "All Files (*.*)|*.*"; openFileDialog1.FilterIndex = 2; openFileDialog1.RestoreDirectory = true; openFileDialog1.ReadOnlyChecked = true; openFileDialog1.ShowReadOnly = true; if (openFileDialog1.ShowDialog() == DialogResult.OK) { tbUpload.Text = openFileDialog1.SafeFileName; string FilePath; FilePath = openFileDialog1.FileName; if (openFileDialog1.FileName != null) { System.IO.FileStream fs = new System.IO.FileStream(FilePath, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite); System.IO.BinaryReader binaryReader = new System.IO.BinaryReader(fs); long byteLength = new System.IO.FileInfo(FilePath).Length; byte[] Attachmnt; Attachmnt = binaryReader.ReadBytes((Int32)byteLength); fs.Close(); fs.Dispose(); binaryReader.Close(); } } }
Now u can store the value of {Attachmnt} through storedprocedure or query into ur database.(But the column where u want to put ur image (Attachmnt) should be type "iamge")
Here i explained it throuth openfiledialog by Browse button,where u will select ur image.
But i have no idea about watermarking through program.
这篇关于水印添加到图像并保存数据库c#(winform)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!