I have a database designed and implemented using ms sql server 2005. initially, the user has entered the pictures with a very big size because there was not restriction. now when the user is opening the crystal report so it takes alot of time and some time memory full problem occur. now i want to reduce the stored images size using vb.net. is it possible? can you help me.. because there is nothing impossible as per my experence.

Yes, it's possible.
First you need to identify which images to resize. You may have to iterate through the entire datatable opening each image and looking at the size.

Once you determine that an image needs to be resized, then you could do something like this:

Dim newWidth As Integer = 640
        Dim newHeight As Integer = 480
        Dim bmp As New Bitmap(<Filename, Stream, etc...>) ' original image, use AppropriateOverload here
        Dim ResizedBMP As New Bitmap(newWidth, newHeight)
        Dim g As Graphics = Graphics.FromImage(bmp)
        g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
        g.DrawImage(bmp, 0, 0, newWidth, newHeight)

        'save the ResizedBMP back to database somewhere after this

Thank You for your concern.
look i have the following code to update the existing image in sql server database

Try
            Dim fs As New FileStream(Trim(txtPath.Text), FileMode.Open)
            Dim Data() As Byte = New [Byte](fs.Length) {}
            fs.Read(Data, 0, fs.Length)
            sql = "UPDATE Biodata Set Pic=@img WHERE No='" & Trim(txtReg.Text) & "' "
            Dim cmd As New SqlCommand(sql)
            cmd.Connection = c.con
            cmd.Parameters.AddWithValue("@img", Data)
            cmd.ExecuteNonQuery()
            c.con.Close()
            fs.Close()
            MsgBox("The Picture has been saved")
        Catch ex As System.Data.SqlClient.SqlException
            MsgBox(ex.Message)
        End Try

now modify it where you would like to bring change which can solve my problem.

Ok, Here you go.

Try
            Dim fs As New FileStream(Trim(txtPath.Text), FileMode.Open)
            Dim Data() As Byte = New [Byte](fs.Length) {}
            fs.Read(Data, 0, fs.Length)
            Dim NewImageAs Byte()
            NewImage= GetResizedImage(Data, 640, 480)
            sql = "UPDATE Biodata Set Pic=@img WHERE No='" & Trim(txtReg.Text) & "' "
            Dim cmd As New SqlCommand(sql)
            cmd.Connection = c.con
            cmd.Parameters.AddWithValue("@img", NewImage)
            cmd.ExecuteNonQuery()
            c.con.Close()
            fs.Close()
            MsgBox("The Picture has been saved")
        Catch ex As System.Data.SqlClient.SqlException
            MsgBox(ex.Message)
        End Try
Private Function GetResizedImage(ByVal Data As Byte(), ByVal NewWidth As Integer, ByVal NewHeight As Integer) As Byte()
        Try
            Dim ms As New IO.MemoryStream(Data)
            Dim bmp As New Bitmap(ms)
            Dim ResizedBMP As New Bitmap(NewWidth, NewHeight)
            Dim g As Graphics = Graphics.FromImage(bmp)
            g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
            g.DrawImage(bmp, 0, 0, NewWidth, NewHeight)
            Dim NewImageStream As New IO.MemoryStream
            bmp.Save(NewImageStream, System.Drawing.Imaging.ImageFormat.Jpeg)
            Return NewImageStream.ToArray
        Catch
            Throw
        End Try
    End Function

See if that helps. Please mark as this question as solved if you feel that it has been.


Thanks!

Hello,
thank you for very simple and cool technique for solving this issue.
there is another issue generated.when i am updating the image so i can store the image in the same size which is specified (640,480) but it overlap the original image in picturebox. I mean the small size picture displays in one cornor of picturebox overlaping the original picture in picturebox. it means the original size of the picture still remain the same including the small size anohter image. which definitly will increase the size of the picture from existing size of the picture.

Can you help me?

Ooops,guess I should have tested first. :)

Try this:

Private Function GetResizedImage(ByVal Data As Byte(), ByVal NewWidth As Integer, ByVal NewHeight As Integer) As Byte()
        Try
            Dim ms As New IO.MemoryStream(Data)
            Dim bmp As New Bitmap(ms)
            Dim ResizedBMP As New Bitmap(NewWidth, NewHeight)
            Dim g As Graphics = Graphics.FromImage(bmp)
            g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
            g.DrawImage(ResizedBMP, 0, 0, NewWidth, NewHeight)
            Dim NewImageStream As New IO.MemoryStream
            ResizedBMP.Save(NewImageStream, System.Drawing.Imaging.ImageFormat.Jpeg)
            Return NewImageStream.ToArray
        Catch
            Throw
        End Try
    End Function

The graphics object should have been created on the ResizedBMP and the return should have been the new BMP.

commented: Thank you for the code but after the size is decreased when view the saved image from database the picture is viewed as black any help. +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.