| | |
Correctly displaying image in PictureBox?
Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Sep 2008
Posts: 27
Reputation:
Solved Threads: 0
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
Thanks,
Sheryl
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.
I tested the code with a 100x100 px box. The code doesn't scale images up to match PictureBox's size, only down.
VB.NET Syntax (Toggle Plain Text)
' See http://www.vb-helper.com/howto_net_image_resize.html for the original code Dim PicBoxHeight As Integer Dim PicBoxWidth As Integer Dim ImageHeight As Integer Dim ImageWidth As Integer Dim TempImage As Image Dim scale_factor As Single PictureBox1.SizeMode = PictureBoxSizeMode.Normal ' Get control size PicBoxHeight = PictureBox1.Height PicBoxWidth = PictureBox1.Width ' Load the image. Change Image.FromFile() to match your code TempImage = Image.FromFile("D:\Download\Image00b.jpg") ' Get image size ImageHeight = TempImage.Height ImageWidth = TempImage.Width ' Calculate scale_factor scale_factor = 1.0 ' No scaling by default i.e. image fits in the Box ' 1) Height If ImageHeight > PicBoxHeight Then ' Reduce height first scale_factor = CSng(PicBoxHeight / ImageHeight) End If ' 2) Width. Notice, we do know now how much we have to scale down the height ' and the scale_factor affects width too If (ImageWidth * scale_factor) > PicBoxWidth Then ' Scaled width exceeds Box's width, recalculate scale_factor scale_factor = CSng(PicBoxWidth / ImageWidth) End If ' Move image to control for resizing PictureBox1.Image = TempImage ' Get the source bitmap. Dim bm_source As New Bitmap(PictureBox1.Image) ' Make a bitmap for the result. Dim bm_dest As New Bitmap( _ CInt(bm_source.Width * scale_factor), _ CInt(bm_source.Height * scale_factor)) ' 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. gr_dest.DrawImage(bm_source, 0, 0, _ bm_dest.Width + 1, _ bm_dest.Height + 1) ' Display the result. PictureBox1.Image = bm_dest
Teme64 @ Windows Developer Blog
•
•
Join Date: Nov 2008
Posts: 7
Reputation:
Solved Threads: 0
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?
![]() |
Other Threads in the VB.NET Forum
- Previous Thread: How to resize images at runtime using mouse...
- Next Thread: query
| Thread Tools | Search this Thread |
.net .net2008 2008 access advanced application array basic beginner browser button buttons center click client code combo convert cuesent data database datagrid datagridview date datetimepicker designer dissertation dissertations dissertationtopic eclipse excel exists fade filter forms function generatetags html images input intel internet listview map mobile module monitor msaccess net number objects open panel pdf picturebox picturebox2 port position print printing problem read regex remove right-to-left save search searchvb.net select serial settings shutdown socket sqldatbase sqlserver survey temperature textbox timer timespan transparency txttoxmlconverter user usercontol vb vb.net vba vbnet visual visualbasic visualbasic.net visualstudio.net visualstudio2008 web winforms winsock wpf wrapingcode xml year





