locked
Resize images in Visual Basic

    Question

  • Hello,

    Can anybody tell me how do I resize a image in Visual Basic? I mean, I have an image with 800x600 px and I want to resize it to 400x300 px and then save it in a new file... The saving part I know how, but I dont know how to resize the image... All this in VB2005.

    Can anybody help me?

    Thank you.

    Tuesday, March 13, 2007 2:11 PM

Answers

  • Arto,

    One thing to remember:
    when resizing images it's size doesn't (always) decrease.
    this is because the 'pixels are stored in the file'.
    Also the size of the image is stored in the file,
    so just changing the size (not cutting away some pixels)
    won't decrease the size of the file
    You are still leaving the same amount of 'pixels' in the file

    Hope this helps..

    Grtz, Tom.

    Tuesday, March 13, 2007 5:25 PM
  • This seems to be a terrribly confusing topic.  GDI+ is one of the best reasons for moving from VB6 to VB.Net.  Put a button on a form and run the following code:

    Option Strict Off

    Option Explicit On

    Public Class Form1

      Private Sub Button1_Click(ByVal sender As System.Object, _

        ByVal e As System.EventArgs) _

        Handles Button1.Click

        Dim L, T, W, H As Integer

        With My.Computer.Screen.WorkingArea

          L = .Left : T = .Top : W = .Width : H = .Height

        End With

        Dim W2 As Integer = W \ 2

        Dim H2 As Integer = H \ 2

        Dim Bmp0 As New Bitmap(W, H)

        Dim g0 As Graphics = Graphics.FromImage(Bmp0)

        g0.CopyFromScreen(L, T, 0, 0, New Size(W, H))

        g0.Dispose()

        Dim Bmp1 As New Bitmap(Bmp0, W2, H2)

        Bmp1.Save("C:\Bmp1.png")

        Dim Bmp2 As New Bitmap(Bmp0.Width \ 2, Bmp0.Height \ 2)

        Dim g1 As Graphics = Graphics.FromImage(Bmp2)

        g1.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic

        g1.DrawImage(Bmp0, 0, 0, Bmp2.Width, Bmp2.Height)

        Bmp2.Save("C:\Bmp2.png")

        g1.Dispose()

      End Sub

    End Class

    Look at the resulting images in Paint, not Picture and Fax viewer (It alters the image).  Change the InterpolationMode.  Look at the file sizes.  Don't forget to delete the files when finished.

    Friday, March 16, 2007 10:51 PM

All replies

  •         Dim w, h As Integer
            w = PictureBox1.Image.Width / 2
            h = PictureBox1.Image.Height / 2
            Dim bmp As Bitmap = New Bitmap(w, h)
            Using g As Graphics = Graphics.FromImage(bmp)
                g.DrawImage(PictureBox1.Image, 0, 0, bmp.Width, bmp.Height)
            End Using
            PictureBox1.Image = bmp
            bmp.Save("c:\NewFileName.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)

    Tuesday, March 13, 2007 3:31 PM
  • Thank you, it worked...

    Although not like i expected! My intention was to reduce its size in the disk - for example, if the picture had 300Kb it would be reduce to 150Kb.

    This code, as amazing as it may seem, made exactly the other way - it increased its size!

    Is this normal?

    Tuesday, March 13, 2007 5:08 PM
  • Arto,

    One thing to remember:
    when resizing images it's size doesn't (always) decrease.
    this is because the 'pixels are stored in the file'.
    Also the size of the image is stored in the file,
    so just changing the size (not cutting away some pixels)
    won't decrease the size of the file
    You are still leaving the same amount of 'pixels' in the file

    Hope this helps..

    Grtz, Tom.

    Tuesday, March 13, 2007 5:25 PM
  • Thanks!

    But how do I do that?

    Tuesday, March 13, 2007 5:29 PM
  • How do you do what?
    I didn't say to do anything did I?

    Grtz, Tom.

    Tuesday, March 13, 2007 5:33 PM
  • Sorry...

    I mean: how do I decrease the image size?

    Tuesday, March 13, 2007 5:37 PM
  • 2 options:

    1) modify the quality of the image
        -> bmp, jpg, png, ..
    2) cut the image
        -> only way to remove pixels

    But don't ask me how to do these,
    as I don't know anything about the drawing in VB

    Grtz, Tom.

    Tuesday, March 13, 2007 5:44 PM
  • 1) Doesn't work. I've tried...

    2)It is not the only way to remove pixels! Take Photoshop for example, it reduces image size without cuting.

    Thanks anyway ;)

    Tuesday, March 13, 2007 5:47 PM
  • Yes,
    but PhotoShop changes the quality of the image,
    so that's like my first option

    Grtz, Tom..

    Tuesday, March 13, 2007 6:13 PM
  • Thanks. But it's the same solution, with same results... It still saves the image  as a larger file than the original!

    Wednesday, March 14, 2007 9:34 AM
  • If you take a 800 x 600 24 bit bitmap which is 1440054 bytes in length and reduce the bitmap to a 400 x 300 24 bit bitmap which is 360054 bytes in length, the reduced file for the smaller bitmap should be smaller.  If it is not, then you are comparing apples and oranges.  Check the size of your files, if they differ in size from the sizes given here then the files are not in bitmap format.  If you want smaller files, you will have to save them in a compressed format.

    Image.Save (String, ImageFormat)

    Wednesday, March 14, 2007 11:45 AM
  •  JohnWein wrote:

    If you take a 800 x 600 24 bit bitmap which is 1440054 bytes in length and reduce the bitmap to a 400 x 300 24 bit bitmap which is 360054 bytes in length, the reduced file for the smaller bitmap should be smaller.  If it is not, then you are comparing apples and oranges.  Check the size of your files, if they differ in size from the sizes given here then the files are not in bitmap format.  If you want smaller files, you will have to save them in a compressed format.

    Image.Save (String, ImageFormat)

    Aparently, it doesn't matter the format in wich i save the image... The size is always the same! Somehow, when I reduce the image size to half, the new image almost double the lenght!

    Thank you all, anyway...

    Friday, March 16, 2007 2:28 PM
  • Show us your code for loading, altering and saving the image.

    Right click your sample image and look at its advanced properties, what is its width, height, resolution and bit depth? What is its file size? Or put it on the web and link to it...

    You can alter the quality settings for certain image formats, but from what you say it sounds like you are doing something wrong already so there's no point confusing things.


    Friday, March 16, 2007 3:56 PM
  •  jo0ls wrote:
    Show us your code for loading, altering and saving the image.

    Right click your sample image and look at its advanced properties, what is its width, height, resolution and bit depth? What is its file size? Or put it on the web and link to it...

    You can alter the quality settings for certain image formats, but from what you say it sounds like you are doing something wrong already so there's no point confusing things.


    Ok. After a few atemps, this is the code for loading the sample image (with 1600x1200 pxs, 24 bits depth, with a resolution of 300ppp W/H and 307Kb of length)

    Dim diag_abrir As New OpenFileDialog

    Dim img As Bitmap

    With diag_abrir

    .DefaultExt = "*.jpg"

    .Filter = "*.jpg | *.jpg"

    .InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyPictures

    .Multiselect = False

    .ShowDialog()

    img = Bitmap.FromFile(.FileName)

    End With

    Dim bmp As Bitmap = New Bitmap(img, 640, 480)

    Dim g As Graphics = Graphics.FromImage(bmp)

    Pic1.Image = bmp

    To save the image:

    With diag_guardar

    .DefaultExt = "*.jpg"

    .Filter = "*.jpg | *.jpg"

    .ShowDialog()

    Pic1.Image.Save(.FileName)

    End With

    This codes produces an image with 640x480 pxs, 32 bitd of depth, 96ppp of W/H resolution and 803Kb of lenght!!

     

    Friday, March 16, 2007 5:00 PM
  • So it's not a bitmap, then. It's a jpeg...

    And it looks lik you are saving it as a bitmap...so it's going to be bigger: it's uncompressed. Try saving it as a jpeg (look at the additional overloads on the 'save' method - re-read JohnWein's post regading the save method.).

    Friday, March 16, 2007 5:17 PM
  •  SJWhiteley wrote:

    So it's not a bitmap, then. It's a jpeg...

    And it looks lik you are saving it as a bitmap...so it's going to be bigger: it's uncompressed. Try saving it as a jpeg (look at the additional overloads on the 'save' method - re-read JohnWein's post regading the save method.).

    I already had tried this before:

    Pic1.Image.Save(.FileName, system.Drawing.Imaging.ImageFormat.Jpeg)

    But it didn't work then... But, with surprise, worked now!!! It reduce the lengh to 46Kb!!!

    Well, it seems that works now!

    Thank you all once more!

    Friday, March 16, 2007 5:33 PM
  • Is your code actually re-sizing the image, or just cutting off the upper-left corner?  I always thought the only way to truly re-size an image is to use GetThumbnailImage method of the Bitmap object and specify the new dimensions (followed by 2 "Nothing" parameters).  I'm not sure how good the image quality is, but it does use some logic in determining what pixel value goes where.
    Friday, March 16, 2007 6:20 PM
  •  Squire James wrote:
    Is your code actually re-sizing the image, or just cutting off the upper-left corner? I always thought the only way to truly re-size an image is to use GetThumbnailImage method of the Bitmap object and specify the new dimensions (followed by 2 "Nothing" parameters). I'm not sure how good the image quality is, but it does use some logic in determining what pixel value goes where.


    As amazing as it may seem, yes. It's re-sizing the picture.

    Try it. Load the picture to a preformated bitmap variable and then save it with the format that you want. As easy as that.

    Sometimes, the simpliest answer is the easiest!
    Friday, March 16, 2007 7:44 PM
  • If you use the overload of DrawImage that draws into a target rectangle, then the image is scaled to fit.

    http://www.bobpowell.net/drawing_an_image.htm
    Friday, March 16, 2007 9:14 PM
  • This seems to be a terrribly confusing topic.  GDI+ is one of the best reasons for moving from VB6 to VB.Net.  Put a button on a form and run the following code:

    Option Strict Off

    Option Explicit On

    Public Class Form1

      Private Sub Button1_Click(ByVal sender As System.Object, _

        ByVal e As System.EventArgs) _

        Handles Button1.Click

        Dim L, T, W, H As Integer

        With My.Computer.Screen.WorkingArea

          L = .Left : T = .Top : W = .Width : H = .Height

        End With

        Dim W2 As Integer = W \ 2

        Dim H2 As Integer = H \ 2

        Dim Bmp0 As New Bitmap(W, H)

        Dim g0 As Graphics = Graphics.FromImage(Bmp0)

        g0.CopyFromScreen(L, T, 0, 0, New Size(W, H))

        g0.Dispose()

        Dim Bmp1 As New Bitmap(Bmp0, W2, H2)

        Bmp1.Save("C:\Bmp1.png")

        Dim Bmp2 As New Bitmap(Bmp0.Width \ 2, Bmp0.Height \ 2)

        Dim g1 As Graphics = Graphics.FromImage(Bmp2)

        g1.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic

        g1.DrawImage(Bmp0, 0, 0, Bmp2.Width, Bmp2.Height)

        Bmp2.Save("C:\Bmp2.png")

        g1.Dispose()

      End Sub

    End Class

    Look at the resulting images in Paint, not Picture and Fax viewer (It alters the image).  Change the InterpolationMode.  Look at the file sizes.  Don't forget to delete the files when finished.

    Friday, March 16, 2007 10:51 PM
  • I noticed it looked like you were saving a 32 bit image.  if you lower the bit depth you will get a smaller image -- it may be that you are loading a lower bit depth image but then saving it as a higher bit depth when you do the save, which would probably make the image get bigger.

     

    Check out the "SetResolution" feature of the bitmap object as in:

    myBitmap.SetResolution()

     

     

    Another approach -- Load the jpg into a picturebox control that is sized as you want it, save the image. You can hide the picturebox if you don't want to see it. Not sure how it does with the image size...

     

     

    Dim bitmap1 As Image

    Try

    bitmap1 = CType(Image.FromFile(imPath), Bitmap)

    Catch ex As System.IO.FileNotFoundException

    MessageBox.Show("There was an error. Check the path to the bitmap.")

    End Try

    bitmap1.RotateFlip(RotateFlipType.Rotate270FlipNone)

    ' Get the scale factor.

    Dim scale_wid As Single = PictureBox1.Width / bitmap1.Width

    Dim scale_hgt As Single = PictureBox1.Height / bitmap1.Height

     

    ' Get the source bitmap.

    ' Make a bitmap for the result.

    Dim bm_dest As New Bitmap( _

    CInt(bitmap1.Width * scale_wid), _

    CInt(bitmap1.Height * scale_hgt))

    ' Make a Graphics object for the result Bitmap.

    Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)

    ' Copy the source image into the destination bitmap.

    ''This makes it show up on the screen and is required for the code to work but I'm not sure why

    gr_dest.DrawImage(bitmap1, 0, 0, bm_dest.Width + 1, bm_dest.Height + 1)

    PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize

    PictureBox1.Image = bm_dest

    PictureBox1.Image.Save(imPath, System.Drawing.Imaging.ImageFormat.Jpeg)

    pctThumb.Visible = True

    pctThumb.Image = bm_dest

    Application.DoEvents()

    bm_dest.Dispose()

    gr_dest.Dispose()

    bitmap1.Dispose()

    Tuesday, April 03, 2007 9:25 PM