位图中的图像大小问题

本文关键字:问题 图像 位图 | 更新日期: 2023-09-27 18:16:02

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

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");
    }

位图中的图像大小问题

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;
}