473,320 Members | 1,691 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,320 developers and data experts.

Stretch image to fit pictureBox at runtime

Richard Alvarez
Ever wonder how to imitate the Image control property stretch in vb6? This piece of code will do the trick...
Expand|Select|Wrap|Line Numbers
  1. Public Sub AutosizeImage(ByVal ImagePath As String, ByVal picBox As PictureBox, Optional ByVal pSizeMode As PictureBoxSizeMode = PictureBoxSizeMode.CenterImage)
  2.         Try
  3.             picBox.Image = Nothing
  4.             picBox.SizeMode = pSizeMode
  5.             If System.IO.File.Exists(ImagePath) Then
  6.                 Dim imgOrg As Bitmap
  7.                 Dim imgShow As Bitmap
  8.                 Dim g As Graphics
  9.                 Dim divideBy, divideByH, divideByW As Double
  10.                 imgOrg = DirectCast(Bitmap.FromFile(ImagePath), Bitmap)
  11.  
  12.                 divideByW = imgOrg.Width / picBox.Width
  13.                 divideByH = imgOrg.Height / picBox.Height
  14.                 If divideByW > 1 Or divideByH > 1 Then
  15.                     If divideByW > divideByH Then
  16.                         divideBy = divideByW
  17.                     Else
  18.                         divideBy = divideByH
  19.                     End If
  20.  
  21.                     imgShow = New Bitmap(CInt(CDbl(imgOrg.Width) / divideBy), CInt(CDbl(imgOrg.Height) / divideBy))
  22.                     imgShow.SetResolution(imgOrg.HorizontalResolution, imgOrg.VerticalResolution)
  23.                     g = Graphics.FromImage(imgShow)
  24.                     g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
  25.                     g.DrawImage(imgOrg, New Rectangle(0, 0, CInt(CDbl(imgOrg.Width) / divideBy), CInt(CDbl(imgOrg.Height) / divideBy)), 0, 0, imgOrg.Width, imgOrg.Height, GraphicsUnit.Pixel)
  26.                     g.Dispose()
  27.                 Else
  28.                     imgShow = New Bitmap(imgOrg.Width, imgOrg.Height)
  29.                     imgShow.SetResolution(imgOrg.HorizontalResolution, imgOrg.VerticalResolution)
  30.                     g = Graphics.FromImage(imgShow)
  31.                     g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
  32.                     g.DrawImage(imgOrg, New Rectangle(0, 0, imgOrg.Width, imgOrg.Height), 0, 0, imgOrg.Width, imgOrg.Height, GraphicsUnit.Pixel)
  33.                     g.Dispose()
  34.                 End If
  35.                 imgOrg.Dispose()
  36.  
  37.                 picBox.Image = imgShow
  38.             Else
  39.                 picBox.Image = Nothing
  40.             End If
  41.  
  42.  
  43.         Catch ex As Exception
  44.             MsgBox(ex.ToString)
  45.         End Try
  46.  
  47.     End Sub
  48.  
====== To use this...=======
first copy the code in a module or somewhere else in your project then...
Expand|Select|Wrap|Line Numbers
  1. AutosizeImage(Trim('" Image file path "'), '" picture control here "', PictureBoxSizeMode.StretchImage)
  2.  
There you have it... :D
Attached Files
File Type: txt mdPhotoFunction.txt (2.3 KB, 853 views)
Oct 4 '11 #1
0 6774

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: hem | last post by:
in my web application, i want to show dynamic images at runtime with crystal report.net. the image is residing in my local computer, i am having problem at runtime i.e. how i give the image path...
7
by: bill.. | last post by:
I have created a VB app that generates a wireless network status map several times a day. The app adds hidden controls in specific areas of the image (on a form) with extra info in the tooltips...
1
by: Lumpierbritches | last post by:
Thank you in advance for any and all assistance. I'm trying to make it where at runtime my program will allow the introduction of an image. I've tried bound, unbound and images. On the bound OLE...
1
by: dcudev.lcr | last post by:
I am loading a 4X5 picturebox matrix using a bitmap file template. As each cell in the matrix is being loaded, I append a text string using the DrawString function to complete the cell's image. ...
1
by: Thomasa Gregg | last post by:
I have a map that I want to add a pushpin image for each location I have in a database. How do I go about adding the pushpin image to the x, y coordinates I have calculated to the map image. I am...
1
by: Tyrant Mikey | last post by:
I ahve a page that contains a DataGrid control that is offset from the left-hand side of the page by about 100 pixels. What I want to do is stretch the DataGrid to fill the remainder of the page...
7
by: Adam Maltby | last post by:
Hi, I am adding an new picbox and animated gif to it at runtime Dim img As Image = Image.FromFile(Application.StartupPath & "\animation.gif") With pic_Wait .Location = New Point(300, 250)...
0
by: Asking | last post by:
hi all, I use TXTextControl (or RichTextBox). I inserted an image on it at runtime and I want to draw some shapes on the image at runtime. How can I do that? (it's windows application) ...
3
by: kulkanuja | last post by:
i am using image control in asp.net in c# now i want to display binary(format) image from database to image control. how do i do iot pls help.
3
by: Jeff Ciaccio | last post by:
I am programming a "craps table", and I would like to randomize where the dice land. The first one is no problem. To fit on the "table" I have the following: picDie1.Location = New Point(Rnd() *...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.