C#学习教程:在c#中裁剪图像当我按下按钮时,我运行了以下代码,但它对图像没有任何作用:try{Imageimage=Image.FromFile("test.jpg");位图bmp=newBitmap(200,200,PixelFormat.Format24bppRgb);bmp。设置分辨率(80,60);图形gfx=Graphics.FromImage(bmp);gfx.SmoothingMode=SmoothingMode.AntiAlias;gfx.InterpolationMode=InterpolationMode.HighQualityBicubic;gfx.PixelOffsetMode=PixelOffsetMode.HighQuality;gfx,DrawRectangle(0,0,200,200),10,10,200,200,GraphicsUnit.Pixel);//Dispose释放资源image.Dispose();bmp.Dispose();gfx.Dispose();}catch(Exceptionex){MessageBox.Show(ex.Message);我的图像实际上是带有以下代码的表单活动窗口的屏幕截图:Rectanglebounds=this.Bounds;使用(Bitmap位图=新位图(bounds.Width,bounds.Height)){使用(Graphicsg=Graphics.FromImage(bitmap)){g.CopyFromScreen(newPoint(bounds.Left,bounds.Top),Point.Empty,bounds.Size);}bitmap.Save("test.jpg",ImageFormat.Jpeg);要完成此操作,请按相同的按钮,首先我想截取表单的屏幕截图,然后裁剪该图像,但裁剪不起作用。这是为什么?您的代码非常接近我用来保存裁剪图像的代码。您缺少保存裁剪图像的部分。您需要将裁剪后的图像写入字节流,然后将其保存到磁盘。我修改了你的代码,它没有经过测试,但试一试。尝试{Imageimage=Image.FromFile("test.jpg");位图bmp=newBitmap(200,200,PixelFormat.Format24bppRgb);bmp.SetResolution(80,60);图形gfx=Graphics.FromImage(bmp);gfx.SmoothingMode=SmoothingMode.AntiAlias;gfx.InterpolationMode=InterpolationMode.HighQualityBicubic;gfx.PixelOffsetMode=PixelOffsetMode.HighQuality;gfx.DrawImage(图像,新矩形(0,0,200,200),10,10,200,200,GraphicsUnit.Pixel);//需要将文件写入内存然后保存MemorySteamms=newMemoryStream();bmp.Save(ms,image.RawFormat);byte[]缓冲区=ms.GetBuffer();varstream=newMemorySteam((buffer),0,buffer.Length);varcroppedImage=SD.Image.FromStream(steam,true);croppedImage.Save("/your/path/image.jpg",croppedImage.RawFormat);//Dispose释放资源image.Dispose();bmp.Dispose();gfx.Dispose();stream.Dispose();croppedImage.Dispose();}catch(Exceptionex){MessageBox.Show(ex.Message);}您可以使用位图类’克隆方法(http://msdn.microsoft.com/en-us/library/ms141944.aspx)获取目标位图的子矩形,你将得到变量bmp的结果。你可以继续尝试。如果您放弃该对象,您所做的更改当然会丢失。我为我的一个项目创建了一个方法,下面是如何尝试,看看是否有效:以上是C#学习教程:Croppinganimageinc#分享全部内容,如果对大家有用需要了解更多关于C#学习教程,希望大家多多关注——位图oBMP=newBitmap(decimal.ToInt16(dWidth),decimal.ToInt16(dHeight));图形g=Graphics.FromImage(oBMP);g.PageUnit=pgUnits;g.SmoothingMode=psMode;g.InterpolationMode=piMode;g.PixelOffsetMode=ppOffsetMode;,0,0,decimal.ToInt16(dWidth),decimal.ToInt16(dHeight));ImageCodecInfooEncoder=GetEncoder();编码器参数oENC=新的编码器参数(1);oENC.Param[0]=newEncoderParameter(System.Drawing.Imaging.Encoder.Quality,plEncoderQuality);oImg.Dispose();oBMP.Save(sOutputFile,oEncoder,oENC);g.处置();}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理会员删除。如需转载请注明出处:
