this is may code on adding or saving image in to mysql DB

Private Sub SAVECATEGORY_Click(sender As Object, e As EventArgs) Handles SAVECATEGORY.Click

  If PictureBox1.Image Is Nothing Then
        MessageBox.Show("Please Insert image ")
        addcategoryclear()                              'you call ddcategoryclear()  to clear textbox and picturebox 
    ElseIf TextBox1.Text = "" Then
        MessageBox.Show("Please Insert Image Name")
        addcategoryclear()                              'you call ddcategoryclear()  to clear textbox and picturebox 

        Dim ms As New MemoryStream  '...........need to Imports System.IO from general
        PictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Png) ' IMAGE

        Dim command As New MySqlCommand("INSERT INTO `category`( `IMG`, `NAME`) VALUES (@IMG,@NAME)", cn)

        With command.Parameters
            .Add("@IMG", MySqlDbType.Blob).Value = ms.ToArray()
            .AddWithValue("NAME", TextBox1.Text)
        End With
        If command.ExecuteNonQuery() = 1 Then
            MessageBox.Show("You Successfully Save")

            addcategoryclear() 'you call ddcategoryclear()  to clear textbox and picturebox 
            MessageBox.Show("Something Wrong Pleas Try again")
        End If
    End If
End Sub

Recommended Answers

All 9 Replies

how i should apply the resize image in may code,this code is from this link can you helpme how to combine this code to my code, thanks

    Dim img As System.Drawing.Image = commonFunctions.byteArrayToImage(DirectCast(context.Session("uploadedImageByteArray"), Byte()))
'Get the Original Format of the Image here
Dim originalFormat As System.Drawing.Imaging.ImageFormat = img.RawFormat

Dim scale_factor As Double = 1

If img.Size.Width > 480 Or img.Size.Height > 320 Then
  If img.Size.Height > img.Size.Width Then
    scale_factor = img.Size.Height / 320
    scale_factor = img.Size.Width / 480
  End If
End If

If scale_factor <> 1 Then
  Dim ScaledWidth As Integer = Convert.ToInt32(img.Size.Width / scale_factor)
  Dim ScaledHeight As Integer = Convert.ToInt32(img.Size.Height / scale_factor)
  'This is the new bitmap you are creating (no Image Format set)
  Dim ScaledDestinationBitmap As Bitmap = New Bitmap(ScaledWidth, ScaledHeight)

  Dim sg As Graphics = Graphics.FromImage(ScaledDestinationBitmap)
  sg.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
  sg.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
  sg.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
  sg.DrawImage(img, 0, 0, ScaledDestinationBitmap.Width, ScaledDestinationBitmap.Height)

  img = ScaledDestinationBitmap
End If
'Pass the original Format here
context.Session("uploadedImageByteArray") = commonFunctions.imageToByteArray(img, originalFormat)

 Public Shared Function imageToByteArray(ByVal imageIn As System.Drawing.Image, ByVal originalFormat As System.Drawing.Imaging.ImageFormat) As Byte()
Dim ms As New MemoryStream()

' Set the format based on the Original image format
imageIn.Save(ms, originalFormat)
Return ms.ToArray()
  End Function

You must know by now that "Resizing images without loss of quality" is not possible. Decrease the pixel count and you lose pixels. Increase the pixel count and the new pixels have to be created by code or guessing.

As such I take it you thought there was something that hasn't been kicked around in the last 3 or more decades of computers. Sorry no. There is always loss. Once you know this, you work out how bad you can accept it.

i notice when i insert image to my picturebox, i see it is looks good but when i save it to my mysqlDB and retrieve it the quality is was reduce but its acceptable for me, but when i try to insert another picture to my picturebox and save it again , it gives an error that the image is huge that cant be insert to my mysqlDB

thats why i look for help about on resize of image, i seen a lot of code im very comfusing how those the image save if there no picturebox on the code

The topic is "Resizing images without loss of quality" so folk may not guess you meant to ask something else.

ya.. thats the topic i seen it here

it didnt know how to apply his code to my code

public void ResizeImage(double scaleFactor, Stream fromStream, Stream toStream)

var image = Image.FromStream(fromStream);
var newWidth = (int)(image.Width * scaleFactor);
var newHeight = (int)(image.Height * scaleFactor);
var thumbnailBitmap = new Bitmap(newWidth, newHeight);

var thumbnailGraph = Graphics.FromImage(thumbnailBitmap);
thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;
thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;

var imageRectangle = new Rectangle(0, 0, newWidth, newHeight);
thumbnailGraph.DrawImage(image, imageRectangle);

thumbnailBitmap.Save(toStream, image.RawFormat);


I see I was unclear. If you pose a question and it's not what you are really asking then folk may not answer your unwritten question.

Take a little more time when you create a discussion next time and ask what you really wanted to ask.

The major methods for image reduction in size are compression, like in jpeg, and resampling. Resampling may take something like a weighted average, which can make things muddy, or select the pixels that are closest to the original position, a center-of-pixel strategy where a minor artifact may grow in relative size. Some strategies do some sort of avaraging that is center-biased. It is nice to keep the original around and generate all rsized images off it, as repeated resampling adds to the muddiness.

Be a part of the DaniWeb community

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