Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Resizing Images in VB.Net

Resizing Images in VB.Net

Scheduled Pinned Locked Moved Visual Basic
csharpcomgraphics
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JoeySmith
    wrote on last edited by
    #1

    What I am trying to do is take an Image (either in the picture box or a saved bitmap) and resize it to my fixed dimensions. Having some trouble with it and the snippets that I've been looking at aren't really helping. Thanks in advance ._._._._._.-.-.-.-.-._._._._._.-.-.-.-.-._._._._._.-.-.-.-.- SicherGrenzen

    clean and elegant

    Y B 2 Replies Last reply
    0
    • J JoeySmith

      What I am trying to do is take an Image (either in the picture box or a saved bitmap) and resize it to my fixed dimensions. Having some trouble with it and the snippets that I've been looking at aren't really helping. Thanks in advance ._._._._._.-.-.-.-.-._._._._._.-.-.-.-.-._._._._._.-.-.-.-.- SicherGrenzen

      clean and elegant

      Y Offline
      Y Offline
      Yuvi Panda
      wrote on last edited by
      #2

      Well, first load the Image and then use the GetThumbNailImage Method to resize it. So, if your Image object is named img, then,

      resizedImage = img.GetThumbNailImage(width, height, Nothing, IntPtr.zero)

      Specify the Width and Height in Pixels, and you'll get your New Image in resizedImage with that height and width. Hope this helps. Yuvi Panda T Microsoft Student Partner Blogs at : http://yuvipanda.blogspot.com

      1 Reply Last reply
      0
      • J JoeySmith

        What I am trying to do is take an Image (either in the picture box or a saved bitmap) and resize it to my fixed dimensions. Having some trouble with it and the snippets that I've been looking at aren't really helping. Thanks in advance ._._._._._.-.-.-.-.-._._._._._.-.-.-.-.-._._._._._.-.-.-.-.- SicherGrenzen

        clean and elegant

        B Offline
        B Offline
        Bhanwar Gupta
        wrote on last edited by
        #3

        hai there, i had made a project on wallpaper changer, and it is working very well. the code PictureBox6.Image = img5.GetThumbnailImage(117, 121, Nothing, Nothing) will made the picturebox as thumbnail of the size demanded. and if you wnat to make the size of the picture according to the resolution ofthe desktop than check this out.... private static void ConvertSourceFileToBmp(string sourceFilePath, string tempBmpFilePath, ScaleStyles scaleStyle, Color backColor) { Image sourceImg = Image.FromFile(sourceFilePath); if (scaleStyle != ScaleStyles.BestFit) { sourceImg.Save(tempBmpFilePath, System.Drawing.Imaging.ImageFormat.Bmp); } else { //get the dimensions of the screen float H = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height; float W = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width; //get the image dimensions float h = sourceImg.Height; float w = sourceImg.Width; //dimensions of target float targetHeight = -1; float targetWidth = -1; //find the appropriate height and width if (H / h >= W / w) { targetWidth = w; } else { targetHeight = h; } if (targetHeight == -1) { targetHeight = (H / W) * targetWidth; } if (targetWidth == -1) { targetWidth = (W / H) * targetHeight; } //create a new image with the default back color with the scaled dimensions w.r.t. image and screen Bitmap bmpImage = new Bitmap((int)targetWidth, (int)targetHeight); Graphics g = Graphics.FromImage(bmpImage); SolidBrush backgroundColorBrush = new SolidBrush(backColor); g.FillRectangle(backgroundColorBrush, 0, 0, bmpImage.Width, bmpImage.Height); //layout this image in the center g.DrawImage(sourceImg, Math.Abs(targetWidth-sourceImg.Width)/2, Math.Abs(targetHeight - sourceImg.Height)/2, sourceImg.Width, sourceImg.Height); //save it as bmp bmpImage.Save(tempBmpFilePath, System.Drawing.Imaging.ImageFormat.Bmp); //dispose stuff backgroundColorBrush.Dispose(); g.Dispose(); bmpImage.Dispose(); } sourceImg.Dispose(); } private static void SetReg

        J 1 Reply Last reply
        0
        • B Bhanwar Gupta

          hai there, i had made a project on wallpaper changer, and it is working very well. the code PictureBox6.Image = img5.GetThumbnailImage(117, 121, Nothing, Nothing) will made the picturebox as thumbnail of the size demanded. and if you wnat to make the size of the picture according to the resolution ofthe desktop than check this out.... private static void ConvertSourceFileToBmp(string sourceFilePath, string tempBmpFilePath, ScaleStyles scaleStyle, Color backColor) { Image sourceImg = Image.FromFile(sourceFilePath); if (scaleStyle != ScaleStyles.BestFit) { sourceImg.Save(tempBmpFilePath, System.Drawing.Imaging.ImageFormat.Bmp); } else { //get the dimensions of the screen float H = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height; float W = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width; //get the image dimensions float h = sourceImg.Height; float w = sourceImg.Width; //dimensions of target float targetHeight = -1; float targetWidth = -1; //find the appropriate height and width if (H / h >= W / w) { targetWidth = w; } else { targetHeight = h; } if (targetHeight == -1) { targetHeight = (H / W) * targetWidth; } if (targetWidth == -1) { targetWidth = (W / H) * targetHeight; } //create a new image with the default back color with the scaled dimensions w.r.t. image and screen Bitmap bmpImage = new Bitmap((int)targetWidth, (int)targetHeight); Graphics g = Graphics.FromImage(bmpImage); SolidBrush backgroundColorBrush = new SolidBrush(backColor); g.FillRectangle(backgroundColorBrush, 0, 0, bmpImage.Width, bmpImage.Height); //layout this image in the center g.DrawImage(sourceImg, Math.Abs(targetWidth-sourceImg.Width)/2, Math.Abs(targetHeight - sourceImg.Height)/2, sourceImg.Width, sourceImg.Height); //save it as bmp bmpImage.Save(tempBmpFilePath, System.Drawing.Imaging.ImageFormat.Bmp); //dispose stuff backgroundColorBrush.Dispose(); g.Dispose(); bmpImage.Dispose(); } sourceImg.Dispose(); } private static void SetReg

          J Offline
          J Offline
          JoeySmith
          wrote on last edited by
          #4

          Thanks for the replies that I recieved. While they helped inresizing the image it wasn't really what I was looking for. I wanted the image resized then saved as the new size. The two snippets suggested simply resized the view of the image. I did, however, find this bit of code which worked nicely after I tailored it a bit. Dim syspath As String syspath = Environ("systemroot") Dim bm As New Bitmap(picLogo.Image) Dim myX As Integer Dim myY As Integer myX = Val(148) myY = Val(119) Dim thumb As New Bitmap(myX, myY) Dim g As Graphics = Graphics.FromImage(thumb) g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic g.DrawImage(bm, New Rectangle(0, 0, myX, myY), New Rectangle(0, 0, bm.Width, _ bm.Height), GraphicsUnit.Pixel) g.Dispose() thumb.Save(syspath & "\system32\oemlogo.bmp", _ System.Drawing.Imaging.ImageFormat.Bmp) bm.Dispose() thumb.Dispose() Me.Close() 'exit app If someone knows of a cleaner/smaller way of doing this feel free to suggest it ._._._._._.-.-.-.-.-._._._._._.-.-.-.-.-._._._._._.-.-.-.-.- Spimoles.NET

          clean and elegant. a beautiful craft

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups