日期:2014-05-17  浏览次数:21017 次

winform中 控件上面画的图如何截取
winForm 程序中,button 事件如下:
Image x = new Image.FromFile("d:\\a.bmp");
pictureBox1.CreateGraphics().DrawImage(x,new Rectangle(0,0,x.Width,x.Height));

点击按钮后,form上出现了图像

我现在想要截取CreateGraphics().DrawImage出来的图像中的一部分,请教大家该如何截取?

------解决方案--------------------
试试先截取控件当前图像
 Control.DrawToBitmap()
------解决方案--------------------
private static Image CutPicture(int X, int Y, int Width, int Height, Image image)
        {
            if (image.Width < X + Width 
------解决方案--------------------
 image.Height < Y + Height)
            {
                MessageBox.Show("截取的区域超过了图片本身的高度、宽度", "错误!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }
            Bitmap Bmp = new Bitmap(image);
            Rectangle cloneRect = new Rectangle(X, Y, Width, Height);
            Bitmap cloneBmp = Bmp.Clone(cloneRect,Bmp.PixelFormat);
            return cloneBmp;
        }

------解决方案--------------------


Bitmap bit = new Bitmap(x.Width,x.Height);
Graphics ig=Graphics.FromImage(bit);
ig.DrawLine(.......
bit.Save(("d:\\b.bmp"); 
这样就能把化的图保存下来了
------解决方案--------------------
上面是对2楼的回复。
如果要实现你的功能效果,可以用Picturebox的MouseUp,MouseDown和MouseMove事件,实现选框的效果,并且得到选择框左上角的x,y坐标和选择矩形的width和height,然后调用3楼的方法实现
------解决方案--------------------
http://blog.csdn.net/welcometousebds/article/details/7776093
可以参考这个
------解决方案--------------------
直接用picturebox.CreateGraphics()得到的画布画出的图像不是pictureBox1.Image。
如4楼思路,用图片得到画布对象Graphics ,然后DrawImage出来后,可以将Bitmap赋给pictureBox1.Image,然后就可以获取了。