Saving Image as jpg with particular size into database


Well-known member
Feb 12, 2006
Programming Experience
Dear All,

I want to save image from picture box into access database with size of 400x300, regardless of whatever the original size of picture in the picture box.

1). For example The Picture may be Desk or Chair. Chair size may be 50x75 varias Desk size might be 400x300. The rest area of Chair picture should be saved as transparent or white space into the database. So when we retreive the picture it won't be distorted.

Can anyone help me to solve this problem please?.

Following is the code which I'm using to save picture into access databse
Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click

  Dim str As New MemoryStream
  'save the image to the stream in .Jpeg format
  pic.Image.Save(str, Imaging.ImageFormat.Jpeg)
  Dim buffer(CInt(str.Length - 1)) As Byte
  str.Position = 0
  str.Read(buffer, 0, CInt(str.Length))

  'create a connection to the images database in the root folder or our machine
  Dim con As New OleDbConnection(conString)
  'create a command object based on this command
  Dim cmd As New OleDbCommand("UPDATE Products SET ProductID = @ProductID,ProductName = @ProductName,CategoryID = @CategoryID,ProductImage = @ProductImage WHERE (ProductID = @ProductID)", con)

  cmd.CommandType = CommandType.Text
  Dim prmPic As New OleDbParameter
  'done step by step here
  With prmPic
    .ParameterName = "@ProductImage" 'the name used in the query for the parameter
    .OleDbType = OleDbType.Binary 'set the database type
    .Value = buffer 'assign the contents of the buffer to the value of the parameter
  End With
  cmd.Parameters.Add("@ProductID", OleDbType.VarChar, 20, "ProductID").Value = txtPartNo.Text
  cmd.Parameters.Add("@ProductName", OleDbType.VarChar, 150, "ProductName").Value = txtDescription.Text
  cmd.Parameters.Add("@CategoryID", OleDbType.Integer, 1, "CategoryID").Value = cboCategory.SelectedIndex
  cmd.Parameters.Add(prmPic) 'add the parameter to the command
    'open the connection
    'execute the command
    'report success and clear the image to allow the user to select another image
    MessageBox.Show("The Record was saved successfully.")
    pic.Image = Nothing
  Catch ex As Exception
    MessageBox.Show("There was a problem saving the Record." & ControlChars.CrLf & ex.Message)
    'close the connection and free the memory we were using
    If con.State = ConnectionState.Open Then con.Close()
    con.Dispose() : cmd.Dispose()
    con = Nothing : cmd = Nothing : prmPic = Nothing
  End Try
End Sub
Thanks a Lot in Advance
Last edited by a moderator:


VB.NET Forum Moderator
Staff member
Dec 17, 2005
Programming Experience
If I understand, the problem is that you don't know how to always save 400x300 even if image is smaller. Jpeg don't support transparent so you fill white. You probably want to center smaller images. Create a new Bitmap, clear it with white color, calculate centering, draw original image centered, then at last save the newly created bitmap that is always 400x300 into DB. Example code:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] bmp [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Bitmap(400, 300)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] g [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Graphics = Graphics.FromImage(bmp)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] sz [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Size = PictureBox1.Image.Size [SIZE=2][COLOR=darkgreen]' PictureBox1.Image is set and displays an image[/COLOR][/SIZE][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] pt [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Point((bmp.Width - sz.Width) \ 2, (bmp.Height - sz.Height) \ 2)
g.DrawImageUnscaled(PictureBox1.Image, pt)
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] str [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] IO.MemoryStream
[/SIZE][COLOR=darkgreen]'save the image to the stream in .Jpeg format[/COLOR]
bmp.Save(str, Imaging.ImageFormat.Jpeg)
Also note that you don't need to transfer the stream data to a new buffer byte array, it's already there in the memorystream, use the GetBuffer method to return the byte array (str.GetBuffer).


Well-known member
Feb 12, 2006
Programming Experience
Thank you very much Mr. John it is Resolved

Mr. John,
Thank you very much now it is resolved. Here I provide the code to help who have come across similar problem.


PrivateFunction CreateThumbnail(ByVal Img As Image, ByVal Width AsInteger, ByVal Height AsInteger, ByVal BackColor As Color) As Bitmap
Dim bmp AsNew Bitmap(Width, Height, PixelFormat.Format24bppRgb)
'--- Scale image to fit the bitmap
Dim imgw AsInteger = Img.Width
Dim imgh AsInteger = Img.Height
If imgw > bmp.Width Then
imgh = imgh * bmp.Width \ imgw
imgw = bmp.Width
If imgh > bmp.Height Then
imgw = imgw * bmp.Height \ imgh
imgh = bmp.Height
'--- Calculate borders
Dim imgl AsInteger = (bmp.Width - imgw) \ 2
Dim imgt AsInteger = (bmp.Height - imgh) \ 1
'--- Draw image into bitmap
Dim g As Graphics = Graphics.FromImage(bmp)
Dim br AsNew SolidBrush(BackColor)
g.FillRectangle(br, 0, 0, bmp.Width, bmp.Height)
g.DrawImage(Img, imgl, imgt, imgw, imgh)
Return bmp

'' Then calling the function and assigning it to stream

Dim thumb As Bitmap = CreateThumbnail(pic.Image, 400, 300, Color.White)
Dim str AsNew MemoryStream
thumb.Save(str, Imaging.ImageFormat.Jpeg)


Top Bottom