我在我的 ASP.NET 应用程序中自动调整图像大小,以便创建该图像的低分辨率缩略图。这段代码工作正常。调整大小后,我尝试向该图像添加“缩略图符号”,例如小放大镜或加号,但结果因图像大小而异。
请注意:原始图像仅调整到一定宽度,因此图像的高度不同。
我的代码如下所示:
private static byte[] InsertThumbnailSign(byte[] imageBuffer, string signPath)
{
byte[] output = null;
MemoryStream stream = new MemoryStream(imageBuffer);
Image image = Image.FromStream(stream);
// Add the thumbnail-sign
Image thumbNailSign = Image.FromFile(signPath);
Graphics graphic = Graphics.FromImage(image);
graphic.DrawImageUnscaled(thumbNailSign, image.Width - thumbNailSign.Width - 4, image.Height - thumbNailSign.Height - 4);
graphic.Flush();
MemoryStream memoryStream = new MemoryStream();
image.Save(memoryStream, ImageFormat.Jpeg);
output = new byte[memoryStream.Length];
memoryStream.Position = 0;
memoryStream.Read(output, 0, (int)memoryStream.Length);
memoryStream.Close();
stream.Dispose();
graphic.Dispose();
memoryStream.Dispose();
return output;
}
在我看来,缩略图标志应该有一个恒定的大小,但事实并非如此。您对如何实现这一目标有任何想法吗?
编辑:刚刚编辑了代码以了解不同的分辨率。但它仍然不起作用:
private static byte[] InsertThumbnailSign(byte[] imageBuffer, string signPath)
{
byte[] output = null;
MemoryStream stream = new MemoryStream(imageBuffer);
Image image = Image.FromStream(stream);
// Add the thumbnail sign with resolution of the containing image
Bitmap t = (Bitmap)Bitmap.FromFile(signPath);
t.SetResolution(image.HorizontalResolution, image.VerticalResolution);
Image thumbNailSign = t;
Graphics graphic = Graphics.FromImage(image);
graphic.DrawImageUnscaled(thumbNailSign, image.Width - thumbNailSign.Width - 4, image.Height - thumbNailSign.Height - 4);
graphic.Flush();
MemoryStream memoryStream = new MemoryStream();
image.Save(memoryStream, ImageFormat.Png);
output = new byte[memoryStream.Length];
memoryStream.Position = 0;
memoryStream.Read(output, 0, (int)memoryStream.Length);
memoryStream.Close();
stream.Dispose();
graphic.Dispose();
memoryStream.Dispose();
return output;
}