Quantcast
Channel: Windows Forms General forum
Viewing all articles
Browse latest Browse all 12583

A generic error occurred in GDI+

$
0
0

Hi,
I got a program which saves some pictures to an Access database. The program works fine with one picture, but gives the above error when trying to load the second and subsequent pictures to the database. I found out this error is given because the system lock of the temp. picture file (m_Path & "\Data_Pic.jpeg"). The code is as follows:

OpenFileDialog.FileName = ""
OpenFileDialog.Title = "Load Picture from disk"
OpenFileDialog.ShowDialog()
If Len(OpenFileDialog.FileName) = 0 Then Exit Sub

Dim original As Image = Image.FromFile(OpenFileDialog.FileName)
Dim resized As Image = ResizeImage(original, New Size(400, 500), True)
Picture1.Image = resized
Picture1.Refresh()
resized.Save(m_Path & "\Data_Pic.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg)

Dim my_stream As New FileStream(m_Path & "\Data_Pic.jpeg", FileMode.Open, FileAccess.Read, FileShare.Read)
Dim bytes_stream(CInt(my_stream.Length) - 1) As Byte
my_stream.Read(bytes_stream, 0, bytes_stream.Length)

Dim myCommand As OleDbCommand = New OleDb.OleDbCommand("Select * from DataPictures")
myCommand.Connection = ConDataPic
myCommand.CommandText = "UPDATE DataPictures SET picPicture = ?" & _" WHERE RecipeCounter = ?"
myCommand.Parameters.AddWithValue("@picPicture", bytes_stream)
myCommand.Parameters.AddWithValue("@RecipeCounter", CLng(lstRecipeID.SelectedItem))

Public Function ResizeImage(ByVal image As Image, ByVal size As Size, Optional ByVal preserveAspectRatio As Boolean = True) As Image
        Dim newWidth As Integer
        Dim newHeight As Integer
        Try
            If preserveAspectRatio Then
                Dim originalWidth As Integer = image.Width
                Dim originalHeight As Integer = image.Height
                Dim percentWidth As Single = CSng(size.Width) / CSng(originalWidth)
                Dim percentHeight As Single = CSng(size.Height) / CSng(originalHeight)
                Dim percent As Single = If(percentHeight < percentWidth, percentHeight, percentWidth)
                newWidth = CInt(originalWidth * percent)
                newHeight = CInt(originalHeight * percent)
            Else
                newWidth = size.Width
                newHeight = size.Height
            End If

            Dim newImage As Image = New Bitmap(newWidth, newHeight)
            Using graphicsHandle As Graphics = Graphics.FromImage(newImage)
                graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic
                graphicsHandle.DrawImage(image, 0, 0, newWidth, newHeight)
            End Using

            Return newImage

        Catch
            MsgBox(Err.Description, MsgBoxStyle.Information, "Resize Image")
            Return image
        End Try
End Function

What I can not figure out, is how to avoid this temp. picture file and just use the filestream object (or such similar). I Would be very happy if someone could point me in the right direction.

Kind Regards
Jorgen


levesen


Viewing all articles
Browse latest Browse all 12583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>