我正在尝试使用openfiledialog选择我选择的图像区域 我想要选择的区域是x,y坐标为5,5的16x16 选择后,我想将16x16图像绘制到坐标0,0
处的另一个pictureBox中这是我已经获得的代码,但我无法选择原始图像的正确部分,有人建议为什么它不起作用?
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK) // Test result.
{
Image origImage = Image.FromFile(openFileDialog1.FileName);
pictureBoxSkin.Image = origImage;
lblOriginalFilename.Text = openFileDialog1.SafeFileName;
System.Drawing.Bitmap bmp = new Bitmap(16, 16);
Graphics g3 = Graphics.FromImage(bmp);
g3.DrawImageUnscaled(origImage, 0, 0, 16, 16);
Graphics g2 = pictureBoxNew.CreateGraphics();
g2.DrawImageUnscaled(bmp, 0, 0, 16, 16);
}
答案 0 :(得分:2)
要选择正确的部分,只需替换:
g3.DrawImageUnscaled(origImage, 0, 0, 16, 16);
与
g3.DrawImageUnscaled(origImage, -5, -5, 16, 16);
答案 1 :(得分:0)
替换这个:
Graphics g2 = pictureBoxNew.CreateGraphics();
g2.DrawImageUnscaled(bmp, 0, 0, 16, 16);
用这个:
pictureBoxNew.Image = bmp;
你没事。
完整代码:
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK) // Test result.
{
Image origImage = Image.FromFile(openFileDialog1.FileName);
pictureBoxSkin.Image = origImage;
lblOriginalFilename.Text = openFileDialog1.SafeFileName;
System.Drawing.Bitmap bmp = new Bitmap(16, 16);
Graphics g3 = Graphics.FromImage(bmp);
g3.DrawImageUnscaled(origImage, 0, 0, 16, 16);
pictureBoxNew.Image = bmp;
//Graphics g2 = pictureBoxNew.CreateGraphics();
//g2.DrawImageUnscaled(bmp, 0, 0, 16, 16);
}
当您在Paint
事件之外的任何地方绘制某些内容时,只要控件需要再次绘制(即从最小化状态恢复,另一个窗口经过时),它将被删除你的窗口,或者你调用Refresh
方法。因此,将控件的绘图代码放在Paint
事件中,或者让控件管理图像的绘制(在这种情况下,将Image
分配给PictureBox
控件。 / p>