首页 > 代码库 > 图片转换为缩略图(Byte->Byte)

图片转换为缩略图(Byte->Byte)

float _thumbnail_X = 0;
float _thumbnail_Y = 0;
int _thumbnail_Width = 480;
int _thumbnail_Height = 480;
//新建内存变量存储原图数据
MemoryStream _sourceImage_MemoryStream = new MemoryStream();
_sourceImage_MemoryStream.Write(sourceByteImage, 0, sourceByteImage.Length);
//绘制原图片
Image _sourceImage = Image.FromStream(_sourceImage_MemoryStream);
//创建缩略图
Image _thumbnail = new Bitmap(_thumbnail_Width, _thumbnail_Height);
//开始绘制缩略图
Graphics _g = Graphics.FromImage(_thumbnail);
RectangleF _sourceRect = new RectangleF(sourceX, sourceY, sourceWidth, sourceHeight);
RectangleF _thumbnailRect = new RectangleF(_thumbnail_X, _thumbnail_Y, _thumbnail_Width, _thumbnail_Height);
_g.DrawImage(_sourceImage, _thumbnailRect, _sourceRect, GraphicsUnit.Pixel);
_g.Save();
//新建内存变量保存缩略图数据
MemoryStream _thumbnail_MemoryStream = new MemoryStream();
_thumbnail.Save(_thumbnail_MemoryStream, ImageFormat.Jpeg);
//新建数据组保存图片数据
byte[] _thumbnailByte = new byte[_thumbnail_MemoryStream.Length];
_thumbnail_MemoryStream.Position = 0;
_thumbnail_MemoryStream.Read(_thumbnailByte, 0, Convert.ToInt32(_thumbnail_MemoryStream.Length));
//关闭、释放资源

_sourceImage_MemoryStream.Dispose();

_sourceImage.Dispose();

_thumbnail.Dispose();

_thumbnail_MemoryStream.Dispose();

_sourceImage_MemoryStream.Close();

_thumbnail_MemoryStream.Close();

图片转换为缩略图(Byte->Byte)