将图像保存在打开的文件上,GDI +中发生了一般错误

时间:2014-12-29 23:59:07

标签: vb.net image gdi

我的代码出现问题,我试图为我的程序制作一个简单的验证码。我第一次运行sub它工作正常,我猜测因为文件不是"打开"在我的程序中。我第二次得到GDI +错误。我很确定这是第二次锁定位图的问题,我只是有点卡住了。

Sub generatePasswordImage(password As String)

        'This is the image where we are going to write the text on it.
        Dim stringMasterImageName As String = Application.StartupPath + "\MasterImage.jpg"
        Dim bitmapImage As Bitmap = New System.Drawing.Bitmap(stringMasterImageName)

        Dim bitmapMasterImage As Bitmap = New System.Drawing.Bitmap(bitmapImage, New Size(400, 150))
        Dim graphicsMasterImage As Graphics = Graphics.FromImage(bitmapMasterImage)

        'Set the alignment based on the coordinates 
        Dim stringformatWriteTextFormat As StringFormat = New StringFormat()
        stringformatWriteTextFormat.Alignment = StringAlignment.Center

        'Do some rotation effects
        Dim Generator As System.Random = New System.Random()
        Dim rotation As Integer = Generator.Next(-10, 5)

        graphicsMasterImage.RotateTransform(rotation)

        Dim centerImgWidth As Integer = Generator.Next(CInt(graphicsMasterImage.VisibleClipBounds.Size.Width / 3), CInt(graphicsMasterImage.VisibleClipBounds.Size.Width / 2))
        Dim centerImgHiehgt As Integer = Generator.Next(CInt(graphicsMasterImage.VisibleClipBounds.Size.Height / 3), CInt(graphicsMasterImage.VisibleClipBounds.Size.Height / 2))

        'Set the font color
        Dim colorStringColor As Color = System.Drawing.Color.Black
        graphicsMasterImage.DrawString(password, New Font("Tahoma", 14, FontStyle.Bold), _
                                       New SolidBrush(colorStringColor), New Point(centerImgWidth, centerImgHiehgt), stringformatWriteTextFormat)

        Dim stringOutPutFileName As String = Application.StartupPath + "\Pass.jpg"
        bitmapMasterImage.Save(stringOutPutFileName, System.Drawing.Imaging.ImageFormat.Jpeg)

        bitmapMasterImage.Dispose()
        bitmapImage.Dispose()

    End Sub

0 个答案:

没有答案