缩略图

context.Response.ContentType = "image/jpeg";

    string path = context.Request.MapPath("03.jpg");
    //原图
    using (Image img = Image.FromFile(path))
    { 
        //保持横纵比例
        int width = 150;
        int height = 100;

        if (img.Width > img.Height)
        {
            height = width * img.Height / img.Width;
        }
        else
        {
            width = height * img.Width / img.Height;
        }
        
        
        //缩略图
        using (Bitmap bitmap = new Bitmap(width, height))
        {
            using (Graphics g = Graphics.FromImage(bitmap))
            {
                g.DrawImage(img, 0, 0,bitmap.Width,bitmap.Height);

                bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
        }
    }

posted on 2015-02-15 22:23  木屐  阅读(101)  评论(0编辑  收藏  举报

导航