日期:2014-05-19  浏览次数:20766 次

gdi+如何复制图片中的一个不规则区域,并将其绘制到其他位置?在线急等
我用GraphicsPath定义了一个区域,但用Graphics.FillPath(Brush,GraphicsPath)只能把这个区域用画笔填充,我现在需要的是把这部分图象复制出来,放到其他位置,并且复制出来的要是图象,而不仅仅是轮廓!
请各位指教!

------解决方案--------------------
//以下示例复制E:\1.JPG的一个椭圆形区域到新建的一个文件E:\2.JPG
Bitmap bm0 = new Bitmap(@ "E:\1.JPG ");
Bitmap bm = new Bitmap(bm0.Width, bm0.Height);
Graphics g=Graphics.FromImage(bm);
GraphicsPath gp = new GraphicsPath();
Rectangle rec = new Rectangle(50,50,120,40);
gp.AddEllipse(rec);
g.SetClip(gp,CombineMode.Replace);
g.DrawImage(bm0,0,0);
bm.Save(@ "E:\2.JPG ");
bm0.Dispose();
bm.Dispose();
------解决方案--------------------
可以做验证代码如下:

Graphics g = Graphics.FromHwnd(this.pictureBox.Handle);
GraphicsPath path = new GraphicsPath();
Image img = Image.FromFile(fileName);

path.AddEllipse(this.pictureBox.ClientRectangle);
g.SetClip(path);
g.DrawImage(img, this.picDraw.ClientRectangle);
g.Dispose();