我有两个问题:
答案 0 :(得分:1)
由于图像是在picbox
缩放的,因此您无法直接从picbox中获取该区域。诀窍是un-scale
用户选择的矩形并将其转换为原始矩形。
您需要两张图片:
Bitmap original600x600; //unscaled
Bitmap picBoxImage; //with 400x400 dimensions
表单加载时:
original600x600 = new Bitmap(600, 600);
//Draw the portion of your 1000x1000 to your 600x600 image
....
....
//create the image of your pictureBox
picBoxImage= new Bitmap(400, 400);
//scale the image in picBoxImage
Graphics gr;
gr = Graphics.FromImage(picBoxImage);
gr.DrawImage(original600x600, new Rectangle(0, 0, 400, 400));
gr.Dispose();
gr = null;
pictureBox1.Image = picBoxImage; //If at any time you want to change the image of
//pictureBox1, you dont't draw directly on the control
//but on picBoxImage and then Invalidate()
当用户选择一个矩形时,我们将其称为rectangleSelect
,在pictureBox1
上,您需要将矩形的x, y, width, height
转换为原始矩形,即600x600。你需要一些简单的数学:
//scaled unscaled with precision
x becomes -----------> x * (600 / 400) (int)( (double)x * (600D / 400D) )
y becomes -----------> y * (600 / 400) (int)( (double)y * (600D / 400D) )
width becomes -----------> width * (600 / 400) (int)( (double)width * (600D / 400D) )
height becomes -----------> height * (600 / 400) (int)( (double)height * (600D / 400D) )
希望这有点帮助!