0

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;
}
Roland
  • 133
  • 2
  • 12

2 Answers2

0

Ensure that all your images are normalized to a consistent DPI/PPI before resizing and/or use that DPI when you create your "sign" image to overlay.

A 300 DPI 2" wide image is 600 pixels wide, where you can also have an image that is 600 pixels wide and have it be over 8" in width if the DPI is set to 72 DPI.

See this question which has a link with example code to get a little more detail about your graphics obj.

Community
  • 1
  • 1
TombMedia
  • 1,962
  • 2
  • 22
  • 27
  • I just ensured that the big image which will contain the thumbnail has a resolution of vertical and horizontal resolution of 96.0f. This is exact the same resolution my 10px*10px thumbnail-sign has. Unfortunately the result is still the same... :-( The debugger ensures that both images has the same resolution... Are there another ideas? Or did i miss a point? – Roland Sep 17 '13 at 13:52
  • Nope if you have gone that route you've done things correctly. – TombMedia Sep 18 '13 at 03:49
0

TombMedia is right. The resolution has been the point.

I discovered an error in my Resizing-Algorithm which caused firefox to resize the image and that caused the different size of the inserted image.

After fixing that error and adapting the resolution it works fine. Thanks for the hint!

Roland
  • 133
  • 2
  • 12