[英]Watermark image doesn't work, get Blank image
我正在尝试在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);
}
水印代码是从这里开始的 。
同样,我要实现的是在图像顶部添加水印文本,而无需在本地写入图像或使用临时的本地图像文件来应用水印。 我得到的是空白图像,而不是带有水印的图像。
您的问题是在方法末尾使用了用于图像的图形。 相反,您必须使用原始图像,因为所有图形均已绘制在其中。
替换此字符串:
Bitmap outputBitMap = new Bitmap(image_small.Width, image_small.Height, grPhoto);
有了这个:
Bitmap outputBitMap = new Bitmap(imageSmall);
祝好运!
您的问题不清楚,如果您需要将生成的图像显示在asp.net的页面中, HttpHandler
是您想要的东西。 这篇文章是希望对您有所帮助的示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.