繁体   English   中英

在VB.net中保存图形但不保存完整图像

[英]Saving a Drawing but not a Full Image in VB.net

我正在VB.Net中制作Paint-esq图像操纵器,但对vb还是很陌生。 我希望用户能够上载图像并对其进行调整,例如添加行和文本。 我还希望用户能够将其添加到其他基本图像的图形和文本转移。 例如,如果用户在公园的图片上方画一条狗,则可以对其进行更改,以使狗改为在街道上。

我一直在把图像加载为picturebox.backgroundImage的想法搞乱了,但是在不重置图形和裁剪图像的情况下更改backgroundImage遇到了困难。 我也一直很讨厌有两个图片框,其中一个放在顶部,用于绘图,但是我遇到了透明度和裁剪问题

这是我用于通过将基本图像设置为.backgroundImage来建立图片框的代码

Private Sub LoadImage(thisImage As Image)

    'we set the picturebox size according to image, we can get image width and height with the help of Image.Width and Image.height properties.
    img.BackgroundImage = thisImage 'c'
    img.Image = New Bitmap(thisImage.Width, thisImage.Height) 'c'

    img.BorderStyle = BorderStyle.FixedSingle
  End Sub

图像处理的例子

 Private Sub ButtonDone_Click(sender As Object, e As EventArgs) Handles ButtonDone.Click, DoneToolStripMenuItem.Click
    Cursor = Cursors.Default
    Select Case LCase(stateFlag)
        Case "header"
            'Reset stuff back to normal
            ButtonHeader.Text = "Header"
            stateFlag = ""
            Cancel_Button.Enabled = False

            'set up space to draw on the image
            Dim newBm As New Bitmap(img.Image.Width, img.Image.Height)

             ' First we define a rectangle with the help of already calculated points 
            Dim newGraphics As Graphics = Graphics.FromImage(newBM) ' create graphics 
            newGraphics.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
            newGraphics.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
            newGraphics.CompositingQuality = Drawing2D.CompositingQuality.HighQuality

            'set image attributes 
            newGraphics.DrawImage(img.Image, New Rectangle(0, 0, img.Image.Width + 1, img.Image.Height + 1), _
                      0, 0, img.Image.Width + 1, img.Image.Height + 1, GraphicsUnit.Pixel)
            'Draw Edges for header

            newGraphics.DrawLine(Pens.Black, startPoint.X, borderSize - 20, startPoint.X, borderSize - 50)
            newGraphics.DrawLine(Pens.Black, endPoint.X, borderSize - 20, endPoint.X, borderSize - 50)

            Dim drawFont As New Font("Times New Roman", 12)
            Dim drawBrush As New SolidBrush(Color.Black)
            Dim stringSize As SizeF = newGraphics.MeasureString(HeaderLabel.Text, drawFont)
            ' Draw header label inbetween the two edges.
            newGraphics.DrawString(HeaderLabel.Text, drawFont, drawBrush, (startPoint.X + endPoint.X) / 2 - (stringSize.Width / 2), borderSize - 45)

            img.Image = newBm
            PushUndo(img.Image.Clone)

结束子

我建议尝试以下方法在另一个图片picturebox上使用一个图片picturebox ,它比其他一些方法简单得多。 表单加载处理程序中 ,执行以下操作:

pctBackground.BackgroundImage = Bitmap.FromFile("park.jpg")
pctForeground.BackColor = Color.Transparent
pctForeground.Parent = pctBackground
pctForeground.Image = New Bitmap(pctForeground.ClientSize.Width, pctForeground.ClientSize.Height)

然后,在pctForegroundpctForeground ,将其保存为:

pctForeground.Image.Save("dog_in_park.png", System.Drawing.Imaging.ImageFormat.Png)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM