CodeItBetter Programming Another VB Programming Blog

How to center a picture in Picture box Control

Posted on August 21, 2009

Let's say you have couple of picture box controls in your form. You have loaded a picture in one of your picture box control Picture2. And, you would like to load the picture loaded in picture 2 into the other picture box control Picture1 and center it. Here is the way:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Option Explicit
 
Public Sub RedrawPic(pict As PictureBox, Source As StdPicture)
  Dim PicWidth As Integer,PicHeight As Integer,NewWidth As Integer,NewHeight As Integer
  Dim CenterX As Integer,CenterY As Integer
  PicHeight = Source.Height / 16.763
  PicWidth = Source.Width / 16.763
  Aspect = PicWidth / PicHeight
  If PicWidth > PicHeight Then
    NewWidth = pict.Width - 240
    NewHeight = pict.Width / Aspect
  Else
    NewWidth = pict.Height * Aspect
    NewHeight = pict.Height - 240
  End If
  CenterX = pict.Width / 2 - NewWidth / 2
  CenterY = pict.Height / 2 - NewHeight / 2
  pict.PaintPicture Source, CenterX, CenterY, NewWidth, NewHeight
End Sub
 
Private Sub Picture1_Click()
  Call RedrawPic(Picture1, Picture2.Picture)
End Sub
Filed under: Controls Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

Trackbacks are disabled.