首页 新闻 搜索 专区 学院

二进制生成图片的问题

0
悬赏园豆:10 [已解决问题] 解决于 2012-09-21 17:30

如何将数据库里的二进制流生成原保存的图片的本地缩略图

xyq_雨晴的主页 xyq_雨晴 | 初学一级 | 园豆:159
提问于:2012-09-17 16:39
< >
分享
最佳答案
0

  /// <summary>
    /// 生成缩略图
    /// </summary>
    /// <param name="imgBuffer">原图byte[]</param>
    /// <param name="width">生成的缩略图宽度</param>
    /// <param name="height">生成的缩略图高度</param>
    /// <returns></returns>
    private byte[] GenerateThumbImg(byte[] imgBuffer,int width,int height)
    {
        MemoryStream imgStream = null;
        MemoryStream thumbStream = new MemoryStream();;
        System.Drawing.Image img = null;
        System.Drawing.Image thumbImg = null;
        System.Drawing.Graphics g = null;
        try
        {
            imgStream = new MemoryStream(imgBuffer);
            img = System.Drawing.Image.FromStream(imgStream);
            thumbImg = new System.Drawing.Bitmap(img, width, height);
            g = System.Drawing.Graphics.FromImage(thumbImg);
            // 设置画布的描绘质量
            g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            g.DrawImage(thumbImg, 0, 0, width, height);
            /*g.DrawImage(img, new System.Drawing.Rectangle(0, 0, width, height),
                0, 0, width, height, System.Drawing.GraphicsUnit.Pixel);*/
            thumbImg.Save(thumbStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            return thumbStream.ToArray();
        }
        catch(Exception ex)
        {
            return null;
        }
        finally
        {
            if (g != null)
                g.Dispose();
            if (thumbImg != null)
                thumbImg.Dispose();
            if (img != null)
                img.Dispose();
            if (thumbStream != null)
                thumbStream.Close();
            if (imgStream != null)
                imgStream.Close();
        }
    }

收获园豆:10
我欲成魔 | 菜鸟二级 |园豆:216 | 2012-09-17 16:58

好像用不了,生成的图片都变样了

xyq_雨晴 | 园豆:159 (初学一级) | 2012-09-18 09:09
其他回答(1)
0
xyq_雨晴 | 园豆:159 (初学一级) | 2012-09-21 17:29
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册