生成缩略图

在原始图片大于设置图片尺寸时生成缩略图。

int _width = 200;
int _height = 200;
var source = System.Drawing.Image.FromStream(new System.IO.MemoryStream(data));
if (source.Height < _height && source.Width < _width) return new System.IO.MemoryStream(data);
double scacle =0.10;
if (((double) source.Height /(double)_height )>= ((double)source.Width/(double)_width))
  scacle = (double)source.Height / (double)_height;
else
  scacle = (double)source.Width / (double)_width;
_width = (int)(source.Width / scacle);
_height = (int)(source.Height / scacle);
//新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(_width, _height);
//新建一个画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空画布并以透明背景色填充
g.Clear(System.Drawing.Color.Transparent);
 g.DrawImage(source,
                new System.Drawing.Rectangle(0, 0, _width, _height),
                new System.Drawing.Rectangle(0, 0, source.Width, source.Height),
                System.Drawing.GraphicsUnit.Pixel);
 var s = new System.IO.MemoryStream();
 bitmap.Save(s, source.RawFormat);

 

posted on 2015-08-20 16:08  lucika.zh  阅读(189)  评论(0)    收藏  举报

导航