创建图形并将其另存为位图

发布于 2024-09-02 13:06:32 字数 252 浏览 4 评论 0原文

我有两个问题:

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

2) 我想将 PictureBox 上创建的 Graphic 转换为 Bitmap 并将其另存为 *.JPG 或 *.bmp。我该怎么做?

I have two questions:

1) I have a PictureBox and its Dock is set to Fill. When I resize the Form I cannot create a Graphic on the part of the PictureBox that is extended. What is the problem?

2) I want to convert the Graphic that is created on the PictureBox to Bitmap and save it as
*.JPG or *.bmp. How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

征棹 2024-09-09 13:06:33

试试这个,对我来说效果很好......

    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)
        {
            //
        }
    }

Try this, works fine for me...

    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)
        {
            //
        }
    }
放低过去 2024-09-09 13:06:33

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

2)您需要从 PictureBox 获取图像并使用其 保存方法

1) Your description is very vague. Do you get an exception? Does it display wrong results? What is happening?

2) You need to get the Image from the PictureBox and use its Save method.

我家小可爱 2024-09-09 13:06:33

当调整 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);

希望有帮助!

When the Picturebox gets resized to fill the form, it seems it's Image property stays the same.

So what you need to do is handle the PictureBox.OnSizeChanged Event, and then use the following code to resize the image:

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

To save the image use:

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

Hope that helps!

与他有关 2024-09-09 13:06:32

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

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

you can use the handle device to get the bitmap out of the picture box

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

or even better, if the pictureBox does`nt modify the image, you can directly get the image from the pictureBox control

pictureBox1.Image.Save("path", System.Drawing.Imaging.ImageFormat.Jpeg);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文