c#生成缩略图 图片固定大小 缩略图加空白填充

public void Upload()

{

if (fileid.PostedFile.FileName != "")

{

//容器高与宽

string backcolor = "#FFFFFF";

string borderColor = "#999999";

int desWidth = 200;

int desHeight = 300;

int penwidth = 0;

int penhight = 0;

int size = fileid.PostedFile.ContentLength;

if (fileid.PostedFile.ContentLength < 512000)

{

string filepath = fileid.PostedFile.FileName;

string name = System.IO.Path.GetFileName(filepath);

int num = filepath.LastIndexOf(".");

string filetype = filepath.Substring(num).ToUpper().Trim();

if (filetype == ".GIF" || filetype == ".PNG" || filetype == ".JPG" || filetype == ".JPEG" || filetype == ".BMP")

{

Byte[] filebyte = new byte[fileid.PostedFile.ContentLength];

System.IO.Stream ostream = this.fileid.PostedFile.InputStream;

System.Drawing.Image oimage = System.Drawing.Image.FromStream(ostream);

int owidth = oimage.Width;

int oheight = oimage.Height;

string hw = GetImageSize(owidth, oheight);

string[] aryhw = hw.Split(';');

int twidth = Convert.ToInt32(aryhw[0]);

int theight = Convert.ToInt32(aryhw[1]);

//新建一个bmp图片

System.Drawing.Bitmap timage = new System.Drawing.Bitmap(desWidth, desHeight);

//新建一个画板

System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(timage);

g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

g.Clear(System.Drawing.ColorTranslator.FromHtml(backcolor));

if (twidth < desWidth & theight == desHeight)

{

penwidth = desWidth - twidth;

}

else if (twidth == desWidth && theight < desHeight)

{

penhight = desHeight - theight;

}

else if (twidth < desWidth && theight < desHeight)

{

penwidth = desWidth - twidth;

penhight = desHeight - theight;

}

int top = penhight / 2;

int left = penwidth / 2;

g.DrawImage(oimage, new System.Drawing.Rectangle(left, top, twidth, theight), new System.Drawing.Rectangle(0, 0, owidth, oheight), System.Drawing.GraphicsUnit.Pixel);

System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.ColorTranslator.FromHtml(borderColor));

g.DrawRectangle(pen,0,0,desWidth-2,desHeight-2);

//string pathifile = Server.HtmlEncode(Request.PhysicalApplicationPath) + "image\\" +"t"+ DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + ".jpg";

string pathifiles = Server.HtmlEncode(Request.PhysicalApplicationPath) + "image\\" + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + ".jpg";

try

{

//原图保存

// oimage.Save(pathifile,System.Drawing.Imaging.ImageFormat.Jpeg);

//缩图图保存

timage.Save(pathifiles, System.Drawing.Imaging.ImageFormat.Jpeg);

}

catch (Exception ex)

{

throw ex;

}

finally

{

g.Dispose();

timage.Dispose();

}

}

}

//获取缩略图的高与宽

protected string GetImageSize(int LoadImgW, int LoadImgH)

{

int xh = 0;

int xw = 0;

//容器高与宽

int oldW = 200;

int oldH = 300;

//图片的高宽与容器的相同

if (LoadImgH == oldH && LoadImgW == (oldW))

{//1.正常显示

xh = LoadImgH;

xw = LoadImgW;

}

if (LoadImgH == oldH && LoadImgW > (oldW))

{//2、原高==容高,原宽>容宽 以原宽为基础

xw = (oldW);

xh = LoadImgH * xw / LoadImgW;

}

if (LoadImgH == oldH && LoadImgW < (oldW))

{//3、原高==容高,原宽<容宽 正常显示

xw = LoadImgW;

xh = LoadImgH;

}

if (LoadImgH > oldH && LoadImgW == (oldW))

{//4、原高>容高,原宽==容宽 以原高为基础

xh = oldH;

xw = LoadImgW * xh / LoadImgH;

}

if (LoadImgH > oldH && LoadImgW > (oldW))

{//5、原高>容高,原宽>容宽

if ((LoadImgH / oldH) > (LoadImgW / (oldW)))

{//原高大的多,以原高为基础

xh = oldH;

xw = LoadImgW * xh / LoadImgH;

}

else

{//以原宽为基础

xw = (oldW);

xh = LoadImgH * xw / LoadImgW;

}

}

if (LoadImgH > oldH && LoadImgW < (oldW))

{//6、原高>容高,原宽<容宽 以原高为基础

xh = oldH;

xw = LoadImgW * xh / LoadImgH;

}

if (LoadImgH < oldH && LoadImgW == (oldW))

{//7、原高<容高,原宽=容宽 正常显示

xh = LoadImgH;

xw = LoadImgW;

}

if (LoadImgH < oldH && LoadImgW > (oldW))

{//8、原高<容高,原宽>容宽 以原宽为基础

xw = (oldW);

xh = LoadImgH * xw / LoadImgW;

}

if (LoadImgH < oldH && LoadImgW < (oldW))

{//9、原高<容高,原宽<容宽//正常显示

xh = LoadImgH;

xw = LoadImgW;

}

return xw + ";" + xh;

}