winform按比例压缩图片

西无 南宁郎程电子科技有限公司 .NET软件开发工程师  2014-08-10 12:40:53
我想要在窗体中选择了图片后不管图片多大都能按比例压缩成我指定的大小然后再转换成二进制数据存到数据库中,但是不知道怎么弄,网上找到的代码能实现转换和存入,就是压缩这块只能单纯切割图片大小,不能按比例压缩。。嗯,你们应该能理解单纯切割和按比例压缩的区别吧?!
...全文
175 点赞 收藏 5
写回复
5 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
於黾 2014-08-11
有拉伸的方法,执行一下就行了
回复
ZhongGuanYao 2014-08-11
上面的要求有两个: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 ; }
回复
xxhhl_2238677164 2014-08-11
上传并按比例生成高清略缩图图片 //按钮上传事件调用生成略缩图程序 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);
回复
yycec 2014-08-10
根据短边的目标尺寸/源尺寸比,计算长边的目标尺寸,按计算后的目标尺寸压缩即可
回复
相关推荐
发帖
C#
创建于2007-09-28

10.5w+

社区成员

.NET技术 C#
申请成为版主
帖子事件
创建了帖子
2014-08-10 12:40
社区公告

让您成为最强悍的C#开发者