Browsing the archives for the GDI tag

丢弃Image类的GetThumbnailImage方法

in Develop

GetThumbnailImage的确是构造一个缩略图最简单的方法但同样存在一些不足:1、如当源图尺寸过大时,生成的缩略图质量会很低,而且同源图的尺寸是一种正比的关系2、当源图是一个Gif图片且含有透明色时,生成的缩略图会将透明色填充成黑色。 今天在工作中就遇到这样的问题,基于上面二个原因,于是决定放弃GetThumbnailImage方法了。最终采用Graphics类的DrawImage方法至于质量问题,适当的设定一下,可以达到很好的效果。 /// </summary> /// <param name=”SourceFile”>文件在服务器上的物理地址</param> /// <param name=”strSavePathFile”>保存在服务器上的路径</param> /// <param name=”ThumbWidth”>宽度</param> /// <param name=”ThumbHeight”>高度</param> /// <param name=”BgColor”>背景</param> public static void myGetThumbnailImage(string SourceFile, string strSavePathFile, int ThumbWidth, int ThumbHeight, string BgColor) { System.Drawing.Image oImg = System.Drawing.Image.FromFile(SourceFile); //小图 int intwidth, intheight; if (oImg.Width > oImg.Height) { if (oImg.Width > ThumbWidth) { intwidth = ThumbWidth; intheight [...]

3 Comments

GDI+中对图片的裁剪已解决

in Develop

没想到Bitmap中的Clone方法可以解决这一切 void CutImage(HttpPostedFile post,string ppuid,out string imagename) { System.Drawing.Image SourceImg = System.Drawing.Image.FromStream(post.InputStream); if (SourceImg.Height > ConfigHelper.UserFaceMaxHeight) { this._lbl_upload_msg.Text = “最大高度不得大于 ” + ConfigHelper.UserFaceMaxHeight; return; } if (SourceImg.Width > ConfigHelper.UserFaceMaxWidth) { this._lbl_upload_msg.Text = “最大宽度不得大于 ” + ConfigHelper.UserFaceMaxWidth; return; } ImageFormat format = getImageformat(System.IO.Path.GetExtension(post.FileName)); string filename = ppuid+”.”+format.ToString(); imagename = filename; if (!UserFaceDir.EndsWith(“\\”)) UserFaceDir = UserFaceDir+”\\”; filename [...]

0 Comments

对Graphics.DrawImageUnscaledAndClipped的疑虑

in Develop

一直没有深入了解研究c#中的GDI+,仅仅只是在应用中构造缩略图或加一下水印在今天的项目中,遇到以下的需求用户在上传图片后,统一调整为64*64;宽高大于64的,从图片中间截取,先不论这种设计合不合理,现在问题出现在对原始图片的裁剪上。.Net中提供了Image类与Graphics类,这二个类可以达到目的。 System.Drawing.Image SourceImg = System.Drawing.Image.FromStream(this.FileUpload1.PostedFile.InputStream); int SourceImgWidth = SourceImg.Width; //图片的原始Width int SourceImgHeight = SourceImg.Height; //图片的原始Height if ((SourceImgWidth != 64) && (SourceImgHeight != 64)) { //System.Drawing.Imaging.ImageFormat f = g.RawFormat; Bitmap b = new Bitmap(64, 64); Graphics gh = Graphics.FromImage(b); gh.DrawImageUnscaledAndClipped(SourceImg, new Rectangle(0, 0, SourceImgWidth, SourceImgHeight)); gh.SmoothingMode = SmoothingMode.AntiAlias; b.Save(@"C:\yy1.jpg"); gh.Dispose(); b.Dispose(); SourceImg.Dispose(); } 查SDK中对于DrawImageUnscaledAndClipped方法的解释:在不进行缩放的情况下绘制指定的图像,并在需要时剪辑该图像以适合指定的矩形。 我的理解是:gh.DrawImageUnscaledAndClipped(SourceImg, new [...]

0 Comments