如何使用位图为多个图像添加水印
How do I add a watermark to multiple images using bitmap
我正在尝试将文本水印添加到目录中的多个图像并将其保存到目录中。这是我目前所拥有的:
using System.Web.Mvc;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
namespace something.Controllers
{
public class something: Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(string text, string text1)
{
DirectoryInfo dir = new DirectoryInfo(@"C:\Users\name\Desktop\Images");
FileInfo[] files = dir.GetFiles();
string value = text;
string value1 = text1;
foreach (FileInfo file in files)
{
using (Bitmap bitmap = new Bitmap( file.FullName))
{
using (Graphics graphics = Graphics.FromImage(bitmap))
{
Brush brush = new SolidBrush(Color.Red);
Font font = new Font("Arial", 10, FontStyle.Italic,
GraphicsUnit.Pixel);
SizeF textSize = new SizeF();
textSize = graphics.MeasureString(value, font);
Point position = new Point(bitmap.Width - ((int)textSize.Width +
30), bitmap.Height - ((int)textSize.Height + 10));
graphics.DrawString((value + value1), font, brush, position);
using (MemoryStream mStream = new MemoryStream())
{
mStream.Position = 0;
bitmap.Save(mStream, ImageFormat.Png);
string _path =
Path.Combine(Server.MapPath("~/UploadedFolders"),
file.Name);
bitmap.Save(_path);
return File(mStream.ToArray(), "image/png", file.Name);
}
}
}
}
return View();
}
}
}
此代码接受用户输入并将其附加到图像中,这部分工作正常。
问题:
使用此代码,它会转到路径并仅向文件夹中的第一张图像添加水印并保存。不是所有的图像。
谢谢
下面的代码有效:
public void watermarkImage(string sourceImage, string text, string targetImage, ImageFormat fmt) {
try
{
// open source image as stream and create a memorystream for output
FileStream source = new FileStream(sourceImage, FileMode.Open);
Stream output = new MemoryStream();
Image img = Image.FromStream(source);
// choose font for text
Font font = new Font("Arial", 20, FontStyle.Bold, GraphicsUnit.Pixel);
//choose color and transparency
Color color = Color.FromArgb(100, 255, 0, 0);
//location of the watermark text in the parent image
Point pt = new Point(10, 5);
SolidBrush brush = new SolidBrush(color);
//draw text on image
Graphics graphics = Graphics.FromImage(img);
graphics.DrawString(text, font, brush, pt);
graphics.Dispose();
//update image memorystream
img.Save(output, fmt);
Image imgFinal = Image.FromStream(output);
//write modified image to file
Bitmap bmp = new System.Drawing.Bitmap(img.Width, img.Height, img.PixelFormat);
Graphics graphics2 = Graphics.FromImage(bmp);
graphics2.DrawImage(imgFinal, new Point(0, 0));
bmp.Save(targetImage, fmt);
imgFinal.Dispose();
img.Dispose();
}
catch (Exception ex) {
MessageBox.Show(ex.Message);
}
}
只需要删除这一行:
return File(mStream.ToArray(), "image/png", file.Name);
代码运行良好,如果有人需要,可以作为另一个参考。
我正在尝试将文本水印添加到目录中的多个图像并将其保存到目录中。这是我目前所拥有的:
using System.Web.Mvc;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
namespace something.Controllers
{
public class something: Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(string text, string text1)
{
DirectoryInfo dir = new DirectoryInfo(@"C:\Users\name\Desktop\Images");
FileInfo[] files = dir.GetFiles();
string value = text;
string value1 = text1;
foreach (FileInfo file in files)
{
using (Bitmap bitmap = new Bitmap( file.FullName))
{
using (Graphics graphics = Graphics.FromImage(bitmap))
{
Brush brush = new SolidBrush(Color.Red);
Font font = new Font("Arial", 10, FontStyle.Italic,
GraphicsUnit.Pixel);
SizeF textSize = new SizeF();
textSize = graphics.MeasureString(value, font);
Point position = new Point(bitmap.Width - ((int)textSize.Width +
30), bitmap.Height - ((int)textSize.Height + 10));
graphics.DrawString((value + value1), font, brush, position);
using (MemoryStream mStream = new MemoryStream())
{
mStream.Position = 0;
bitmap.Save(mStream, ImageFormat.Png);
string _path =
Path.Combine(Server.MapPath("~/UploadedFolders"),
file.Name);
bitmap.Save(_path);
return File(mStream.ToArray(), "image/png", file.Name);
}
}
}
}
return View();
}
}
}
此代码接受用户输入并将其附加到图像中,这部分工作正常。 问题: 使用此代码,它会转到路径并仅向文件夹中的第一张图像添加水印并保存。不是所有的图像。 谢谢
下面的代码有效:
public void watermarkImage(string sourceImage, string text, string targetImage, ImageFormat fmt) {
try
{
// open source image as stream and create a memorystream for output
FileStream source = new FileStream(sourceImage, FileMode.Open);
Stream output = new MemoryStream();
Image img = Image.FromStream(source);
// choose font for text
Font font = new Font("Arial", 20, FontStyle.Bold, GraphicsUnit.Pixel);
//choose color and transparency
Color color = Color.FromArgb(100, 255, 0, 0);
//location of the watermark text in the parent image
Point pt = new Point(10, 5);
SolidBrush brush = new SolidBrush(color);
//draw text on image
Graphics graphics = Graphics.FromImage(img);
graphics.DrawString(text, font, brush, pt);
graphics.Dispose();
//update image memorystream
img.Save(output, fmt);
Image imgFinal = Image.FromStream(output);
//write modified image to file
Bitmap bmp = new System.Drawing.Bitmap(img.Width, img.Height, img.PixelFormat);
Graphics graphics2 = Graphics.FromImage(bmp);
graphics2.DrawImage(imgFinal, new Point(0, 0));
bmp.Save(targetImage, fmt);
imgFinal.Dispose();
img.Dispose();
}
catch (Exception ex) {
MessageBox.Show(ex.Message);
}
}
只需要删除这一行:
return File(mStream.ToArray(), "image/png", file.Name);
代码运行良好,如果有人需要,可以作为另一个参考。