更改图片大小 怎样用C#程序写一个能改变图片大小的程序,!!!谢谢!!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 #region 图片上传 ///<summary> ///方法名称:upLogd ///内容摘要:图片上传 /// </summary> /// <param name="path">源图路径(物理路径) </param> /// <param name="spath">缩略图路径(物理路径) </param> /// <param name="fu">图片上传控件 </param> /// <param name="w">缩略图宽度 </param> /// <param name="h">缩略图高度 </param> public string upLoad(string Path, string sPath, FileUpload fu, int w, int h) { //判断上传控件中是否有值 if (fu.HasFile == false) { return "请选择要上传的图片!"; } else { string fType = fu.PostedFile.ContentType;//获取图像的类型 if (fType == "image/bmp" || fType == "image/gif" || fType == "image/pjpeg" || fType == "image/jpeg" || fType == "image/x-png") { //获取文件信息 FileInfo file = new FileInfo(fu.PostedFile.FileName); System.DateTime currentTime = new System.DateTime(); currentTime = DateTime.Now; string stamp = currentTime.Year.ToString() + currentTime.Month.ToString() + currentTime.Day.ToString() + currentTime.Hour.ToString() + currentTime.Minute.ToString() + currentTime.Second.ToString() + currentTime.Millisecond.ToString(); //原始图片保存路径 string path = Path + stamp + ".gif"; //缩略图保存路径 string spath = sPath + "Thumb_" + stamp + ".gif"; try { //原始图片保存 fu.SaveAs(path); //缩略图保存 if (w != 0 && h != 0) { MakeThumbImage(path, spath, w, h); } return "图片上传成功|" + stamp + ".gif"; } catch(Exception ex) { return "图片上传失败"+ex.Message.ToString(); } } else { return "上传图片格式不正确!请上传jpg/jpge/gif/bmp/png格式的图片!"; } } } #endregion #region 图片压缩 /// <summary> ///方法名称:MakeThumbImage ///内容摘要:生成缩略图 /// </summary> /// <param name="sPath">源图路径(物理路径) </param> /// <param name="stPath">缩略图路径(物理路径) </param> /// <param name="nWidth">缩略图宽度 </param> /// <param name="nHeight">缩略图高度 </param> private void MakeThumbImage(string sPath, string stPath, int nWidth, int nHeight) { System.Drawing.Image sImage = System.Drawing.Image.FromFile(sPath); int tw = nWidth; int th = nHeight; ///原始图片的宽度和高度 int sw = sImage.Width; int sh = sImage.Height; if (sw > tw) { sw = tw; } if (sh > th) { sh = th; } System.Drawing.Bitmap objPic, objNewPic; objPic = new System.Drawing.Bitmap(sPath); objNewPic = new System.Drawing.Bitmap(objPic, sw, sh); objNewPic.Save(stPath); sImage.Dispose(); objPic.Dispose(); objNewPic.Dispose(); } #endregion 图片缩略图public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode) { Image originalImage = Image.FromFile(originalImagePath); int towidth = width; int toheight = height; int x = 0; int y = 0; int ow = originalImage.Width; int oh = originalImage.Height; switch (mode) { case "HW": break; case "W": toheight = originalImage.Height * width/originalImage.Width; break; case "H": towidth = originalImage.Width * height/originalImage.Height; break; case "Cut": if((double)originalImage.Width/(double)originalImage.Height > (double)towidth/(double)toheight) { oh = originalImage.Height; ow = originalImage.Height*towidth/toheight; y = 0; x = (originalImage.Width - ow)/2; } else { ow = originalImage.Width; oh = originalImage.Width*height/towidth; x = 0; y = (originalImage.Height - oh)/2; } break; default : break; } Image bitmap = new System.Drawing.Bitmap(towidth,toheight); Graphics g = System.Drawing.Graphics.FromImage(bitmap); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.Clear(Color.Transparent); g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight), new Rectangle(x, y, ow,oh), GraphicsUnit.Pixel); try { bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg); } catch(System.Exception e) { throw e; } finally { originalImage.Dispose(); bitmap.Dispose(); g.Dispose(); } } C#打印datagridview里面的内容 关于bho插件中添加文本框和button,求教 恳求指出程序错误 请看数据类型转换的问题 连接mysql报错,急,在线等!!! 怎样将datatable中的一列的数据放入一个数组 一个从来不曾注意的问题,在C#语言中,对于字符串变量的赋初值问题,大家发表一下见解!! 怎么获得当前页面的 URL ? C# 2.0中如何例示一个堆栈? 如何做NET的安装程序,让其自动安装NET运行时和初始化数据库。 高分求解,winform&WPF 求条SQL语句
#region 图片上传
///<summary>
///方法名称:upLogd
///内容摘要:图片上传
/// </summary>
/// <param name="path">源图路径(物理路径) </param>
/// <param name="spath">缩略图路径(物理路径) </param>
/// <param name="fu">图片上传控件 </param>
/// <param name="w">缩略图宽度 </param>
/// <param name="h">缩略图高度 </param>
public string upLoad(string Path, string sPath, FileUpload fu, int w, int h)
{ //判断上传控件中是否有值
if (fu.HasFile == false)
{
return "请选择要上传的图片!";
}
else
{
string fType = fu.PostedFile.ContentType;//获取图像的类型
if (fType == "image/bmp" || fType == "image/gif" || fType == "image/pjpeg" || fType == "image/jpeg" || fType == "image/x-png")
{ //获取文件信息
FileInfo file = new FileInfo(fu.PostedFile.FileName); System.DateTime currentTime = new System.DateTime();
currentTime = DateTime.Now;
string stamp = currentTime.Year.ToString() + currentTime.Month.ToString() + currentTime.Day.ToString() + currentTime.Hour.ToString() + currentTime.Minute.ToString() + currentTime.Second.ToString() + currentTime.Millisecond.ToString(); //原始图片保存路径
string path = Path + stamp + ".gif";
//缩略图保存路径
string spath = sPath + "Thumb_" + stamp + ".gif";
try
{ //原始图片保存
fu.SaveAs(path);
//缩略图保存
if (w != 0 && h != 0)
{
MakeThumbImage(path, spath, w, h);
}
return "图片上传成功|" + stamp + ".gif";
}
catch(Exception ex)
{
return "图片上传失败"+ex.Message.ToString();
}
}
else
{
return "上传图片格式不正确!请上传jpg/jpge/gif/bmp/png格式的图片!";
}
}
}
#endregion #region 图片压缩
/// <summary>
///方法名称:MakeThumbImage
///内容摘要:生成缩略图
/// </summary>
/// <param name="sPath">源图路径(物理路径) </param>
/// <param name="stPath">缩略图路径(物理路径) </param>
/// <param name="nWidth">缩略图宽度 </param>
/// <param name="nHeight">缩略图高度 </param>
private void MakeThumbImage(string sPath, string stPath, int nWidth, int nHeight)
{
System.Drawing.Image sImage = System.Drawing.Image.FromFile(sPath);
int tw = nWidth;
int th = nHeight;
///原始图片的宽度和高度
int sw = sImage.Width;
int sh = sImage.Height;
if (sw > tw)
{
sw = tw;
}
if (sh > th)
{
sh = th;
}
System.Drawing.Bitmap objPic, objNewPic;
objPic = new System.Drawing.Bitmap(sPath);
objNewPic = new System.Drawing.Bitmap(objPic, sw, sh);
objNewPic.Save(stPath);
sImage.Dispose();
objPic.Dispose();
objNewPic.Dispose();
} #endregion
public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
{
Image originalImage = Image.FromFile(originalImagePath);
int towidth = width;
int toheight = height;
int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height;
switch (mode)
{
case "HW":
break;
case "W":
toheight = originalImage.Height * width/originalImage.Width;
break;
case "H":
towidth = originalImage.Width * height/originalImage.Height;
break;
case "Cut":
if((double)originalImage.Width/(double)originalImage.Height > (double)towidth/(double)toheight)
{
oh = originalImage.Height;
ow = originalImage.Height*towidth/toheight;
y = 0;
x = (originalImage.Width - ow)/2;
}
else
{
ow = originalImage.Width;
oh = originalImage.Width*height/towidth;
x = 0;
y = (originalImage.Height - oh)/2;
}
break;
default :
break;
}
Image bitmap = new System.Drawing.Bitmap(towidth,toheight);
Graphics g = System.Drawing.Graphics.FromImage(bitmap);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.Clear(Color.Transparent);
g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
new Rectangle(x, y, ow,oh),
GraphicsUnit.Pixel); try
{
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch(System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}