Frage

I have made an httphandler that shrinks an image and returns it as PNG. This works fine on my Vista PC with IE 9, but not on some old XP machines with IE 8. It seems strange that this should be a browser issue, but to me it looks like that. But, I am thinking that since I produce the PNG on the server, I must do something wrong in the code.

The httphandler (simplified):

<%@ WebHandler Language="C#" Class="ShowPicture" %>
using System.Data;
using System;
using System.IO;
using System.Web;

public class ShowPicture : IHttpHandler {

public void ProcessRequest (HttpContext context) {
    context.Response.ContentType = "image/png";
    // byteArray comes from database
    // maxWidth and maxHeight comes from Request
    context.Response.BinaryWrite(
        Common.ResizeImageFromArray(byteArray, maxWidth, maxHeight));
}

And the function called (simplified as well):

public static byte[] ResizeImageFromArray(byte[] array, int maxWidth, int maxHeight)
{
    byte[] picArray = array;

    if (maxWidth > 0 || maxHeight > 0)  // Resize the image
    {
        Bitmap dbbmp = (Bitmap)Bitmap.FromStream(new MemoryStream(array));

        if (dbbmp.Width > maxWidth || dbbmp.Height > maxHeight)
        {
            // Calculate the max width/height factor

            Bitmap resized = new Bitmap(dbbmp, newWidth, newHeight);
            MemoryStream ms = new MemoryStream();
            resized.Save(ms, System.Drawing.Imaging.ImageFormat.Png);

            picArray = new Byte[ms.Length - 1];
            ms.Position = 0;
            ms.Read(picArray, 0, picArray.Length);
            ms.Close();
        }
    }
    return picArray;
}

I appreciate any ideas and/or input. Thanks in advance.

War es hilfreich?

Lösung

To resize with different format:

Bitmap resizedBitmap = new Bitmap(newWidth, newHeight, PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(resizedBitmap);
g.DrawImage(originalBitmap, new Rectangle(Point.Empty, resizedBitmap.Size), new Rectangle(Point.Empty, originalBitmap.Size), GraphicsUnit.Pixel);
g.Dispose();

With that you will have yopur scaled bitmap, also you can play with the Graphics object options to get better quality/faster processing times.

Also, to convert your MemoryStream to array is better to use ms.ToArray();

picArray = ms.ToArray();

This way you don't need to create the array by yourself.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top