winform按比例压缩图片 winform压缩二进制图片 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://blog.sina.com.cn/s/blog_6427a6b50101el7v.html 上传并按比例生成高清略缩图图片//按钮上传事件调用生成略缩图程序 protected void btnUpload_Click(object sender, EventArgs e) { if (FileUploadImage.PostedFile.FileName != "") { //定义上传路径(在当前目录下的uploadfile文件下) string uploadpath = this.Server.MapPath("uploadfile"); //取得文件名 string tmpfilename = FileUploadImage.PostedFile.FileName; //文件名 filename = tmpfilename.Substring(tmpfilename.LastIndexOf("\\") + 1); //原文件的保存路径 string fileSavePath = uploadpath + "\\" + filename; //保存原图片 FileUploadImage.SaveAs(fileSavePath); //调用生成缩略图程序,生成缩略图及生成写字的图片 this.toImage(FileUploadImage.PostedFile.InputStream, uploadpath, filename); //求取后缀名 string suffix = filename.Substring(filename.LastIndexOf(".")); //显示图片 //分别为原图片/写字的图片(多一个w)/缩略图(多一个x) this.imgshow.ImageUrl = "~/uploadfile/" + filename; this.imgpri.ImageUrl = "~/uploadfile/" + filename.Replace(suffix, "w" + suffix); this.imgSmall.ImageUrl = "~/uploadfile/" + filename.Replace(suffix, "x" + suffix); //显示图像控件 this.imgshow.Visible = true; this.imgpri.Visible = true; this.imgSmall.Visible = true; } }//生成略缩图程序 private void toImage(Stream myStream, string uploadPath, string picName) { //取得后缀名 string suffix = picName.Substring(picName.LastIndexOf(".")); //缩略图的保存路径 string fileXltPath = uploadPath + "\\" + picName.Replace(suffix, "x" + suffix); //写字图的保存路径 string fileXztPath = uploadPath + "\\" + picName.Replace(suffix, "w" + suffix); //创建一个图像对象取得上传图片对象 System.Drawing.Image myImage = System.Drawing.Image.FromStream(myStream, false); //对绘制前的图片产生一个缩略图(原图片一半大小) System.Drawing.Image thumbImage = myImage.GetThumbnailImage(myImage.Size.Width / 2, myImage.Size.Height / 2, null, System.IntPtr.Zero); //保存缩略图 thumbImage.Save(fileXltPath, this.getImageFormat(suffix)); //关闭缩略图对象 thumbImage.Dispose(); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(myImage); g.DrawImage(myImage, 0, 0, myImage.Size.Width, myImage.Size.Height); //选择字体及字体大小 System.Drawing.Font f = new Font("隶书", 40); //定义字体颜色 System.Drawing.Brush b = new SolidBrush(System.Drawing.Color.Red); //开始绘制,根据上述两种设定,添加绘制的上左位置 g.DrawString("远大宏图", f, b, 10, 10); //关闭绘制对象 g.Dispose(); //保存绘制后上传图片 myImage.Save(fileXztPath, myImage.RawFormat); //关闭图片对象 myImage.Dispose(); }//CodeGo.net/// 根据图片的后缀名,返回要保存的图片格式 private System.Drawing.Imaging.ImageFormat getImageFormat(string suffix) { System.Drawing.Imaging.ImageFormat myFormat; switch (suffix.ToLower()) { case ".bmp": myFormat = System.Drawing.Imaging.ImageFormat.Bmp; break; case ".emf": myFormat = System.Drawing.Imaging.ImageFormat.Emf; break; case ".exif": myFormat = System.Drawing.Imaging.ImageFormat.Exif; break; case ".gif": myFormat = System.Drawing.Imaging.ImageFormat.Gif; break; case ".icon": myFormat = System.Drawing.Imaging.ImageFormat.Icon; break; case ".jpeg": case ".jpg": myFormat = System.Drawing.Imaging.ImageFormat.Jpeg; break; case ".png": myFormat = System.Drawing.Imaging.ImageFormat.Png; break; case ".tiff": myFormat = System.Drawing.Imaging.ImageFormat.Tiff; break; case ".wmf": myFormat = System.Drawing.Imaging.ImageFormat.Wmf; break; default: myFormat = System.Drawing.Imaging.ImageFormat.MemoryBmp; break; } return (myFormat); 上面的要求有两个:1)固定的大小 2)图片按比例缩小或扩大固定的大小存在三种情况:高>宽 高<宽 高=宽添加的图片也存在三种情况:高>宽 高<宽 高=宽解决方法是比较固定大小的宽高比例和图片宽高比例//image为源图片,w为固定的宽,h为固定的高private Image GetSizeImage(Image image, int w, int h) { double pCtrl = (double)w / (double)h;//固定的宽高比例 double pImg = (double)image.Width / (double)image.Height;//图片宽高比例 Image newImage; if (pCtrl > pImg) { if (h < image.Height) { newImage = drawImage(image, (int)(h * pImg), h); } else { newImage = drawImage(image, image.Width, image.Height); } } else if (pCtrl < pImg) { if (image.Width > w) { newImage = drawImage(image, w, (int)(w / pImg)); } else { newImage = drawImage(image, image.Width, image.Height); } } else { if (image.Width > w) { newImage = drawImage(image,w, h); } else { newImage = drawImage(image, image.Width, image.Height); } } return newImage ; } private Image drawImage(Image sourceImage, int width, int height) { Image bitmap = new Bitmap(width, height); Graphics img = Graphics.FromImage(bitmap); img.CompositingQuality = CompositingQuality.HighQuality; img.SmoothingMode = SmoothingMode.HighQuality; img.InterpolationMode = InterpolationMode.High; img.DrawImage(sourceImage, new Rectangle(0, 0, width, height)); return bitmap ; } C# WinForm 怎样动态修改CheckListBox的Item值 多功能计算器里如何处理TextBox里输入的多个运算符的数学表达式 企业级c/s该怎么设计? 读取dat和xml的问题 C#自定义用户控件调用时与JAVASCRPT应用配合的问题。 .net 如何调用这段SQL语句? 如何在C#代码中对VSS完成CHECK OUT和CHECKIN 操作 如何让DataGrid显示的时间完整??? WINFORM+WEB SERVICE做报表系统的方案可行吗? 求高手 c#程序怎么修改另一个c#程序中的变量?求助 C#winform窗体画矩形问题
//按钮上传事件调用生成略缩图程序
protected void btnUpload_Click(object sender, EventArgs e)
{
if (FileUploadImage.PostedFile.FileName != "")
{
//定义上传路径(在当前目录下的uploadfile文件下)
string uploadpath = this.Server.MapPath("uploadfile");
//取得文件名
string tmpfilename = FileUploadImage.PostedFile.FileName;
//文件名
filename = tmpfilename.Substring(tmpfilename.LastIndexOf("\\") + 1);
//原文件的保存路径
string fileSavePath = uploadpath + "\\" + filename;
//保存原图片
FileUploadImage.SaveAs(fileSavePath);
//调用生成缩略图程序,生成缩略图及生成写字的图片
this.toImage(FileUploadImage.PostedFile.InputStream, uploadpath, filename);
//求取后缀名
string suffix = filename.Substring(filename.LastIndexOf("."));
//显示图片
//分别为原图片/写字的图片(多一个w)/缩略图(多一个x)
this.imgshow.ImageUrl = "~/uploadfile/" + filename;
this.imgpri.ImageUrl = "~/uploadfile/" + filename.Replace(suffix, "w" + suffix);
this.imgSmall.ImageUrl = "~/uploadfile/" + filename.Replace(suffix, "x" + suffix);
//显示图像控件
this.imgshow.Visible = true;
this.imgpri.Visible = true;
this.imgSmall.Visible = true;
}
}
//生成略缩图程序
private void toImage(Stream myStream, string uploadPath, string picName)
{
//取得后缀名
string suffix = picName.Substring(picName.LastIndexOf("."));
//缩略图的保存路径
string fileXltPath = uploadPath + "\\" + picName.Replace(suffix, "x" + suffix);
//写字图的保存路径
string fileXztPath = uploadPath + "\\" + picName.Replace(suffix, "w" + suffix);
//创建一个图像对象取得上传图片对象
System.Drawing.Image myImage = System.Drawing.Image.FromStream(myStream, false);
//对绘制前的图片产生一个缩略图(原图片一半大小)
System.Drawing.Image thumbImage = myImage.GetThumbnailImage(myImage.Size.Width / 2, myImage.Size.Height / 2, null, System.IntPtr.Zero);
//保存缩略图
thumbImage.Save(fileXltPath, this.getImageFormat(suffix));
//关闭缩略图对象
thumbImage.Dispose();
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(myImage);
g.DrawImage(myImage, 0, 0, myImage.Size.Width, myImage.Size.Height);
//选择字体及字体大小
System.Drawing.Font f = new Font("隶书", 40);
//定义字体颜色
System.Drawing.Brush b = new SolidBrush(System.Drawing.Color.Red);
//开始绘制,根据上述两种设定,添加绘制的上左位置
g.DrawString("远大宏图", f, b, 10, 10);
//关闭绘制对象
g.Dispose();
//保存绘制后上传图片
myImage.Save(fileXztPath, myImage.RawFormat);
//关闭图片对象
myImage.Dispose();
}//CodeGo.net/
// 根据图片的后缀名,返回要保存的图片格式
private System.Drawing.Imaging.ImageFormat getImageFormat(string suffix)
{
System.Drawing.Imaging.ImageFormat myFormat;
switch (suffix.ToLower())
{
case ".bmp":
myFormat = System.Drawing.Imaging.ImageFormat.Bmp;
break;
case ".emf":
myFormat = System.Drawing.Imaging.ImageFormat.Emf;
break;
case ".exif":
myFormat = System.Drawing.Imaging.ImageFormat.Exif;
break;
case ".gif":
myFormat = System.Drawing.Imaging.ImageFormat.Gif;
break;
case ".icon":
myFormat = System.Drawing.Imaging.ImageFormat.Icon;
break;
case ".jpeg":
case ".jpg":
myFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
break;
case ".png":
myFormat = System.Drawing.Imaging.ImageFormat.Png;
break;
case ".tiff":
myFormat = System.Drawing.Imaging.ImageFormat.Tiff;
break;
case ".wmf":
myFormat = System.Drawing.Imaging.ImageFormat.Wmf;
break;
default:
myFormat = System.Drawing.Imaging.ImageFormat.MemoryBmp;
break;
}
return (myFormat);
上面的要求有两个:1)固定的大小 2)图片按比例缩小或扩大
固定的大小存在三种情况:高>宽 高<宽 高=宽
添加的图片也存在三种情况:高>宽 高<宽 高=宽
解决方法是比较固定大小的宽高比例和图片宽高比例//image为源图片,w为固定的宽,h为固定的高
private Image GetSizeImage(Image image, int w, int h)
{
double pCtrl = (double)w / (double)h;//固定的宽高比例
double pImg = (double)image.Width / (double)image.Height;//图片宽高比例 Image newImage;
if (pCtrl > pImg) {
if (h < image.Height) {
newImage = drawImage(image, (int)(h * pImg), h);
} else {
newImage = drawImage(image, image.Width, image.Height);
}
} else if (pCtrl < pImg) {
if (image.Width > w) {
newImage = drawImage(image, w, (int)(w / pImg));
} else {
newImage = drawImage(image, image.Width, image.Height);
}
} else {
if (image.Width > w) {
newImage = drawImage(image,w, h);
} else {
newImage = drawImage(image, image.Width, image.Height);
}
} return newImage ;
}
private Image drawImage(Image sourceImage, int width, int height)
{
Image bitmap = new Bitmap(width, height);
Graphics img = Graphics.FromImage(bitmap);
img.CompositingQuality = CompositingQuality.HighQuality;
img.SmoothingMode = SmoothingMode.HighQuality;
img.InterpolationMode = InterpolationMode.High;
img.DrawImage(sourceImage, new Rectangle(0, 0, width, height)); return bitmap ;
}