VB.Net图片与背景保存

时间:2014-09-23 06:03:57

标签: vb.net image visual-studio-2010

我需要将包含其背景的PictureBox保存到硬盘中的JPG文件中; 到目前为止,我可以从Resources加载图片,然后将PictureBox保存到硬盘驱动器,但它会删除背景; 我的代码如下:

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    PictureBox1.Image = My.Resources.ResourceManager.GetObject("test.jpg")
    Dim Image As New Bitmap(800, 400)
    Dim MekSaveImage As Graphics = Graphics.FromImage(Image)
    For Parts = 0 To 3
        RRRR = 10 + Parts * 198
        Dim RectPen As New Pen(Color.Red)
        RectPen.Width = 2
        Dim rect As New Rectangle(RRRR, 10, 188, 300)
        MekSaveImage.DrawRectangle(RectPen, rect)
    Next
    PictureBox1.Image = Image
    PictureBox1.Image.Save("C:\Test\Test.jpg", Imaging.ImageFormat.Jpeg)
End Sub

提前感谢您的帮助/评论。

1 个答案:

答案 0 :(得分:1)

所以我猜你在test.jpg中有你的Backgroundimage,并想在上面绘制矩形。

从图像开始。 Ressource本身没有改变。

Dim BackgroundImage As Bitmap = My.Resources.ResourceManager.GetObject("test.jpg")

从此图像创建图形对象,并在顶部绘制矩形。您不需要从新图像创建图形图像。您可以使用现有的那些将用作画布。

Using g As Graphics = Graphics.FromImage(BackgroundImage)
    'Draw the Rectangles
    Using RectPen As New Pen(Color.Red)
        RectPen.Width = 2
        For Parts = 0 To 3
            RRRR = 10 + Parts * 198
            Dim rect As New Rectangle(RRRR, 10, 188, 300)
            g.DrawRectangle(RectPen, rect)
        Next
    End Using 'RectPen
End Using 'g
PictureBox1.Image = BackgroundImage  'Display the result
BackgroundImage.Save("C:\Test\Test.jpg", Imaging.ImageFormat.Jpeg) 'Save to disk.

我正在使用Using块来确保处理图形对象和笔的资源。