发新帖

[C/VC/C++] C++怎么把一块图片二进制数据显示成图片

零下一度 2022-12-9 708
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();
            }
        }


@博客园


最新回复 (0)
返回
零下一度
主题数
934
帖子数
0
注册排名
1