3

画像をトリミングしており、ashx ハンドラーを使用して画像を返したいと考えています。トリミングコードは次のとおりです。

public static System.Drawing.Image Crop(string img, int width, int height, int x, int y)
    {
        try
        {
            System.Drawing.Image image = System.Drawing.Image.FromFile(img);
            Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);
            bmp.SetResolution(image.HorizontalResolution, image.VerticalResolution);

            Graphics gfx = Graphics.FromImage(bmp);
            gfx.SmoothingMode = SmoothingMode.AntiAlias;
            gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
            gfx.PixelOffsetMode = PixelOffsetMode.HighQuality;
            gfx.DrawImage(image, new Rectangle(0, 0, width, height), x, y, width, height, GraphicsUnit.Pixel);
            // Dispose to free up resources
            image.Dispose();
            bmp.Dispose();
            gfx.Dispose();

            return bmp;
        }
        catch (Exception ex)
        {
            return null;
        }
    }

ビットマップが返されます。物理ファイルを作成したくないので、コンテキスト ストリームを介してブラウザに送信する必要があります。

4

3 に答える 3

1

応答ストリームにビットマップを書き込みます (そして正しい MIME タイプを設定します)

サイズを小さくするためにpng/jpgに変換するのもいいかもしれません

于 2009-06-23T07:34:36.740 に答える