这个问题好像看到很久了,不知道哪里出了问题,随便试了一下,好像没啥问题的样子
下面代码实现 以鼠标为中心,300 * 300的方框,点击进行截取
(方框超出picturebox范围的逻辑未实现)
private void pictureBox1_Click(object sender, EventArgs e)
{
int w = pictureBox1.Width;
int h = pictureBox1.Height;
Bitmap bm = new Bitmap(w, h);
pictureBox1.DrawToBitmap(bm, new Rectangle(0, 0, w, h));
//bm.Save("E:\\1.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); //int x = Math.Max(0, ((MouseEventArgs)e).X - WIDTH/2);
//int y = Math.Max(0, ((MouseEventArgs)e).Y - HEIGHT / 2);
int x = ((MouseEventArgs)e).X - WIDTH / 2;
int y = ((MouseEventArgs)e).Y - HEIGHT / 2;
using (Bitmap bmp = new Bitmap(WIDTH, HEIGHT, PixelFormat.Format32bppRgb))
{
using (Graphics g = Graphics.FromImage(bmp))
{
g.Clear(Color.White);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.DrawImage(bm, new Rectangle(0, 0, WIDTH, HEIGHT),
new Rectangle(x, y, WIDTH, HEIGHT), GraphicsUnit.Pixel);
bmp.Save("E:\\2.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
g.Dispose();
}
}
bm.Dispose();
} private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
pictureBox1.Refresh();
Graphics g = pictureBox1.CreateGraphics();
g.DrawRectangle(new Pen(new SolidBrush(Color.Green)), new Rectangle(e.Location.X - WIDTH / 2, e.Location.Y - HEIGHT/2, WIDTH, HEIGHT));
g.Dispose();
}
下面代码实现 以鼠标为中心,300 * 300的方框,点击进行截取
(方框超出picturebox范围的逻辑未实现)
private void pictureBox1_Click(object sender, EventArgs e)
{
int w = pictureBox1.Width;
int h = pictureBox1.Height;
Bitmap bm = new Bitmap(w, h);
pictureBox1.DrawToBitmap(bm, new Rectangle(0, 0, w, h));
//bm.Save("E:\\1.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); //int x = Math.Max(0, ((MouseEventArgs)e).X - WIDTH/2);
//int y = Math.Max(0, ((MouseEventArgs)e).Y - HEIGHT / 2);
int x = ((MouseEventArgs)e).X - WIDTH / 2;
int y = ((MouseEventArgs)e).Y - HEIGHT / 2;
using (Bitmap bmp = new Bitmap(WIDTH, HEIGHT, PixelFormat.Format32bppRgb))
{
using (Graphics g = Graphics.FromImage(bmp))
{
g.Clear(Color.White);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.DrawImage(bm, new Rectangle(0, 0, WIDTH, HEIGHT),
new Rectangle(x, y, WIDTH, HEIGHT), GraphicsUnit.Pixel);
bmp.Save("E:\\2.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
g.Dispose();
}
}
bm.Dispose();
} private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
pictureBox1.Refresh();
Graphics g = pictureBox1.CreateGraphics();
g.DrawRectangle(new Pen(new SolidBrush(Color.Green)), new Rectangle(e.Location.X - WIDTH / 2, e.Location.Y - HEIGHT/2, WIDTH, HEIGHT));
g.Dispose();
}
我用的是很大的图片,然后zoom的形式在picturebox中显示的
可以把你的源文件给我看看吗,
我用的是很大的图片,然后zoom的形式在picturebox中显示的
可以把你的源文件给我看看吗,上面发的就是源码啊,就这么几句话就可以了
随便拖一个picturebox,设置zoom显示,然后指定张大图片,把上面两个事件加上
效果就是另外一个帖子你说的所见即所得
picturebox显示zoom之后,截取的也是zoom之后的
我把整个工程文件上传到资源了,你可以下载试试看 (好像 CSDN还没有check通过,过会儿有链接了,我再补充一下)
如果你不是VS2012,可以自己转换为你自己的工程来试
效果如下:原始图From1:所见即所得,截300*300大小的图From2:按照框子选中的范围,截取原始图对应的部分两个Form的窗口都可以Resize,你可以试试看
昨晚开始上传了四遍了,每次都是上传成功,但到现在一个都没显示出来传到百度了http://pan.baidu.com/s/1jGzII2M