位图中的图像大小调整问题

时间:2013-05-29 05:48:08

标签: c#

我使用以下代码创建位图。图像正在创建,但没有指定的大小。代码有什么问题吗?

public class ProcessImage : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        var bmpOut = new Bitmap(100, 100);
        var request = (HttpWebRequest)WebRequest.Create("http://media.zenfs.com/en_us/Sports/ap/201305061639599673916-p2.jpeg");
        request.AllowWriteStreamBuffering = true;
        var response = (HttpWebResponse) request.GetResponse();
        var myStream = response.GetResponseStream();
        var b = new Bitmap(myStream);
        var g = Graphics.FromImage(bmpOut);
        g.InterpolationMode = InterpolationMode.High;
        g.DrawImage(b,0,0,50, 50);
        context.Response.ContentType = "image/png";
        var stream = new MemoryStream();
        b.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
        b.Dispose();
        stream.WriteTo(HttpContext.Current.Response.OutputStream);
        //context.Response.Write("Hello World");
    }

1 个答案:

答案 0 :(得分:3)

private Bitmap ResizeImage(Image image, int width, int height)
{
    Bitmap result = new Bitmap(width, height);
    result.SetResolution(image.HorizontalResolution, image.VerticalResolution);
    using (Graphics graphics = Graphics.FromImage(result))
    {
        graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
        graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

        graphics.DrawImage(image, 0, 0, result.Width, result.Height);
    }
    return result;
}