【发布时间】:2013-12-14 23:09:32
【问题描述】:
我正在尝试在 ASP.NET 中的图像上应用水印文本。我想使用图像流而不是使用/写入本地文件。
代码如下:
using (MemoryStream ms_small = new MemoryStream())
{
System.Drawing.Image image_small = System.Drawing.Image.FromStream(filestream);
width = image_small.Width;
height = image_small.Height;
string Copyright = "Hi I am copyright watermark";
Graphics grPhoto = Graphics.FromImage(image_small);
int phWidth = image_small.Width;
int phHeight = image_small.Height;
grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
int[] sizes = new int[] { 16, 14, 12, 10, 8, 6, 4 };
Font crFont = null;
SizeF crSize = new SizeF();
for (int i = 0; i < 7; i++)
{
crFont = new Font("arial", sizes[i], FontStyle.Bold);
crSize = grPhoto.MeasureString(Copyright, crFont);
if ((ushort)crSize.Width < (ushort)phWidth)
break;
}
int yPixlesFromBottom = (int)(phHeight * .05);
float yPosFromBottom = ((phHeight - yPixlesFromBottom) - (crSize.Height / 2));
float xCenterOfImg = (phWidth / 2);
StringFormat StrFormat = new StringFormat();
StrFormat.Alignment = StringAlignment.Center;
SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(153, 0, 0, 0));
grPhoto.DrawString(Copyright,
crFont,
semiTransBrush2,
new PointF(xCenterOfImg + 1, yPosFromBottom + 1),
StrFormat);
SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(153, 255, 255, 255));
grPhoto.DrawString(Copyright,
crFont,
semiTransBrush,
new PointF(xCenterOfImg, yPosFromBottom),
StrFormat);
Bitmap outputBitMap = new Bitmap(image_small.Width, image_small.Height, grPhoto);
image_small = (Image)outputBitMap;
ImageBuilder.Current.Build(image_small, ms_small, rs_small_jpg);
}
水印代码来自here。
再次,我想要实现的是图像顶部的水印文本,而不是在本地写入图像或使用临时本地图像文件来应用水印。我得到的是一张空白图片,而不是带有水印的图片。
【问题讨论】:
-
生成图像后,您希望如何处理它?你的代码是在 HttpHandler 里面还是什么?
-
我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。