8

我有两个问题:

1)我有一个PictureBox,它的 Dock 设置为 Fill。当我调整大小时,Form我无法在PictureBox扩展的部分创建图形。问题是什么?

2) 我想将在PictureBoxto上创建的 Graphic 转换Bitmap为 *.JPG 或 *.bmp。我怎样才能做到这一点?

4

4 回答 4

7

您可以使用手柄设备 将位图从图片框中取出

Graphics g = pictureBox1.CreateGraphics();          
Bitmap bitMap = Bitmap.FromHbitmap(g.GetHdc());
bitMap.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);

甚至更好,如果pictureBox不修改图片,可以直接从pictureBox控件中获取图片

pictureBox1.Image.Save("path", System.Drawing.Imaging.ImageFormat.Jpeg);
于 2010-05-21T10:16:00.380 回答
2

试试这个,对我来说很好用......

    private void SaveControlImage(Control ctr)
    {
        try
        {
            var imagePath = @"C:\Image.png";

            Image bmp = new Bitmap(ctr.Width, ctr.Height);
            var gg = Graphics.FromImage(bmp);
            var rect = ctr.RectangleToScreen(ctr.ClientRectangle);
            gg.CopyFromScreen(rect.Location, Point.Empty, ctr.Size);

            bmp.Save(imagePath);
            Process.Start(imagePath);

        }
        catch (Exception)
        {
            //
        }
    }
于 2016-05-20T07:29:46.450 回答
0

1)你的描述很模糊。你有例外吗?它是否显示错误的结果?怎么了?

2)您需要从 PictureBox 中获取 Image 并使用其Save 方法

于 2010-05-21T09:31:10.630 回答
0

当 Picturebox 调整大小以填充表单时,它的 Image 属性似乎保持不变。

所以你需要做的是处理 PictureBox.OnSizeChanged 事件,然后使用下面的代码来调整图像的大小:

private void pictureBox1_SizeChanged(object sender, EventArgs e)
{
    if ((pictureBox1.Image != null))
    {
        pictureBox1.Image = new Bitmap(pictureBox1.Image, pictureBox1.Size);
    }
}

要保存图像,请使用:

pictureBox1.Image.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg);

希望有帮助!

于 2010-05-21T10:18:17.807 回答