i am automatically resizing images in my ASP.NET application in order to create a low resolution thumbnail of that image. This code is working fine. After resizing i am trying to add a "thumbnail-sign", for example an small loupe or a plus, to that image, but the result differs depending on the image size.
Please note: The Original Image is only resized to a certain width, so the images differs in height.
My code looks like this:
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;
}
In my opinion the thumbnail sign should have a constant size, but that is not the case. Do you have any ideas how to achieve this?
EDIT: Just edited the code to be aware of different resolutions. But it still does not work:
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;
}