Correctly displaying image in PictureBox?

Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2008
Posts: 27
Reputation: Sheryl99 is an unknown quantity at this point 
Solved Threads: 0
Sheryl99 Sheryl99 is offline Offline
Light Poster

Correctly displaying image in PictureBox?

 
0
  #1
Oct 14th, 2008
I am using a PictureBox to display images. I have SizeMode set to StretchImage. While this does indeed show the whole image, it is of coure stretched. Is there a way to prepare the image to be displayed so it will fit correctly in the PictureBox? I just want it displayed without distortion and within the box. Then perhaps I could set the SizeMode to Normal.

Thanks,
Sheryl
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 114
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: Correctly displaying image in PictureBox?

 
0
  #2
Oct 14th, 2008
You have to scale the image. The code below gets an image from the file and rescales its size down to fit in the PictureBox.

  1. ' See http://www.vb-helper.com/howto_net_image_resize.html for the original code
  2. Dim PicBoxHeight As Integer
  3. Dim PicBoxWidth As Integer
  4. Dim ImageHeight As Integer
  5. Dim ImageWidth As Integer
  6. Dim TempImage As Image
  7. Dim scale_factor As Single
  8.  
  9. PictureBox1.SizeMode = PictureBoxSizeMode.Normal
  10. ' Get control size
  11. PicBoxHeight = PictureBox1.Height
  12. PicBoxWidth = PictureBox1.Width
  13. ' Load the image. Change Image.FromFile() to match your code
  14. TempImage = Image.FromFile("D:\Download\Image00b.jpg")
  15. ' Get image size
  16. ImageHeight = TempImage.Height
  17. ImageWidth = TempImage.Width
  18. ' Calculate scale_factor
  19. scale_factor = 1.0 ' No scaling by default i.e. image fits in the Box
  20. ' 1) Height
  21. If ImageHeight > PicBoxHeight Then
  22. ' Reduce height first
  23. scale_factor = CSng(PicBoxHeight / ImageHeight)
  24. End If
  25. ' 2) Width. Notice, we do know now how much we have to scale down the height
  26. ' and the scale_factor affects width too
  27. If (ImageWidth * scale_factor) > PicBoxWidth Then
  28. ' Scaled width exceeds Box's width, recalculate scale_factor
  29. scale_factor = CSng(PicBoxWidth / ImageWidth)
  30. End If
  31.  
  32. ' Move image to control for resizing
  33. PictureBox1.Image = TempImage
  34.  
  35. ' Get the source bitmap.
  36. Dim bm_source As New Bitmap(PictureBox1.Image)
  37. ' Make a bitmap for the result.
  38. Dim bm_dest As New Bitmap( _
  39. CInt(bm_source.Width * scale_factor), _
  40. CInt(bm_source.Height * scale_factor))
  41. ' Make a Graphics object for the result Bitmap.
  42. Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)
  43. ' Copy the source image into the destination bitmap.
  44. gr_dest.DrawImage(bm_source, 0, 0, _
  45. bm_dest.Width + 1, _
  46. bm_dest.Height + 1)
  47. ' Display the result.
  48. PictureBox1.Image = bm_dest
I tested the code with a 100x100 px box. The code doesn't scale images up to match PictureBox's size, only down.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 27
Reputation: Sheryl99 is an unknown quantity at this point 
Solved Threads: 0
Sheryl99 Sheryl99 is offline Offline
Light Poster

Re: Correctly displaying image in PictureBox?

 
0
  #3
Oct 15th, 2008
Wow, it really works -- and there were no errors in your code.

Thank you!
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 7
Reputation: cutieann12 is an unknown quantity at this point 
Solved Threads: 0
cutieann12 cutieann12 is offline Offline
Newbie Poster

Re: Correctly displaying image in PictureBox?

 
0
  #4
Feb 21st, 2009
Hello, i've tried the sample code..above however this code works only in a single image..i am doing application which needs a resize module for all the images i've loaded using the radiobuttons.. what i want is that when the user clicks on the images in the radiobuttons it could be resize..say i click pic1 then resize, then i click pic2 again i could resize it..any idea how to do this?
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the VB.NET Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC