1

我正在尝试选择使用 openfiledialog 选择的图像区域我尝试选择的区域是 x,y 坐标 5,5 的 16x16 选择后,我想将 16x16 图像绘制到坐标 0 的另一个图片框中,0

这是我得到的代码,但我无法让它选择原始图像的正确部分,有人对它为什么不起作用有任何建议吗?

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);
}
4

2 回答 2

2

为了选择正确的部分,只需替换:

g3.DrawImageUnscaled(origImage, 0, 0, 16, 16);

g3.DrawImageUnscaled(origImage, -5, -5, 16, 16);
于 2013-01-26T14:27:43.497 回答
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);
}

当您在event之外的Paint任何地方绘制某些东西时,它会在需要再次绘制控件时立即删除(即,当从最小化状态恢复时,另一个窗口会越过您的窗口,或者您调用该Refresh方法)。因此,将控件的绘图代码放在它们的Paint事件中,或者让控件管理图像的绘制(在这种情况下,将 an 分配ImagePictureBox控件)。

于 2013-01-26T14:17:46.630 回答