Resizing Images in VB.Net
-
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
-
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
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 -
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
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
-
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
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.NETclean and elegant. a beautiful craft