Rotate jpg/tif

Posted on 2004-04-24
Last Modified: 2013-11-19
I have an Image box in a form, whose image is populated in VBA as follows:

Image26.Picture = LoadPicture("c:\hi.jpg")

How can I get it to load rotated 90 degrees?

I am aware that;en-us;q186260 allows a bitmap to be rotated 90 degrees, but it doesn't work for jpgs or tifs.

Many thanks.

Question by:PhillipBurton
  • 2
  • 2
  • 2
LVL 13

Expert Comment

ID: 10909231
Why do you want to use an "Image Box" and why not a "Picture Box"..... The image box donot have hDC which severely limits the use of API to rotate the image.......


Author Comment

ID: 10911038
If I could use a PictureBox, who would I do the rotation?

Accepted Solution

Burbble earned 400 total points
ID: 10912261
You mind find use in the code here:

To avoid the little "spiral" effect of drawing an image at an angle, remove the t% = DoEvents() line in the bmp_rotate() sub, and set Picture2.AutoRedraw = True.

You can use any angle, just change the Theta value of this line in Command3_Click() sub...

Call bmp_rotate(Picture1, Picture2, 3.14 / 4) whatever angle you want (in radians -- i.e. 3.14 / 2 = 90°).

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

LVL 13

Assisted Solution

imarshad earned 100 total points
ID: 10969449
You can also use this code
Add a Picture Box control on the form and add a JPG onto the Picture  box and paste the following code

Private Const DIB_RGB_COLORS = 0 '  color table in RGBs
Private Type RGBQUAD
        rgbBlue As Byte
        rgbGreen As Byte
        rgbRed As Byte
        rgbReserved As Byte
End Type
Private Type BITMAPINFOHEADER '40 bytes
        biSize As Long
        biWidth As Long
        biHeight As Long
        biPlanes As Integer
        biBitCount As Integer
        biCompression As Long
        biSizeImage As Long
        biXPelsPerMeter As Long
        biYPelsPerMeter As Long
        biClrUsed As Long
        biClrImportant As Long
End Type
        bmiHeader As BITMAPINFOHEADER
        bmiColors As RGBQUAD
End Type
Private Declare Function GetDIBits Lib "gdi32" (ByVal aHDC As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As BITMAPINFO, ByVal wUsage As Long) As Long
Private Declare Function SetDIBits Lib "gdi32" (ByVal hdc As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As BITMAPINFO, ByVal wUsage As Long) As Long

Private Sub Form_Load()
'Dim fn As String
'fn = App.Path
'If Right$(fn, 1) <> "\" Then fn = fn & "\"
'fn = fn & "vb.jpg"
picInitial = LoadPicture() 'Enter the Path of Picture to be rotated here
picRot = picInitial
picRot.Move 0, 0, picInitial.Height, picInitial.Width
End Sub

Private Sub picInitial_Click()
Dim bits() As RGBQUAD
Dim x As Long, y As Long
Dim aRot() As RGBQUAD
Dim mx As Long, my As Long
'//Redim our array to the size of the picturebox
mx = picInitial.ScaleWidth - 1
my = picInitial.ScaleHeight - 1
ReDim bits(mx, my)
ReDim aRot(my, mx)
With bmi.bmiHeader
    .biSize = 40
    .biPlanes = 1
    .biBitCount = 32
    .biCompression = 0
    .biClrUsed = 0
    .biClrImportant = 0
    .biSizeImage = (mx + 1) * (my + 1)
    .biWidth = mx + 1
    .biHeight = my + 1
End With
x = GetDIBits(picInitial.hdc, picInitial.Image.Handle, 0, my + 1, bits(0, 0), bmi, DIB_RGB_COLORS)
For x = 0 To mx
    For y = 0 To my
        aRot(my - y, x) = bits(x, y)
    Next y
Next x
With bmi.bmiHeader
    .biSize = 40
    .biPlanes = 1
    .biBitCount = 32
    .biCompression = 0
    .biClrUsed = 0
    .biClrImportant = 0
    .biSizeImage = (mx + 1) * (my + 1)
    .biWidth = my + 1
    .biHeight = mx + 1
End With
x = SetDIBits(picRot.hdc, picRot.Image.Handle, 0, bmi.bmiHeader.biHeight, aRot(0, 0), bmi, DIB_RGB_COLORS)
picInitial.Visible = False
End Sub

Author Comment

ID: 11263970
Burbble's solution seems quick, if one eplaces pic1hDC%, pic1hDC% with pic1hDC&, pic1hDC&.

Whilst  imarshad's solution looks good, it seems to take ages with a fairly big graphic.

Thanks to everyone who helped.

Expert Comment

ID: 11264861
Glad you got it working, thanks for the points.


Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Best way to parse out a json string in VB6? 10 293
How to incorporate an error traping routing to existing code in VB6 11 49
Change size 15 68
transition to visual .net from vb6 5 63
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
In this tutorial viewers will learn how to create blended and gradiated shapes in Illustrator using the blend tool Draw two shapes, one of them in a different color: Select both and create a blend by going to Object > Blend > Make: Blends can also b…
Not only does Prezi allow you to create non-linear presentations, it also makes it easy to apply transition animations between your frames. Learn how to apply a fade-in transition to select items and entire frames in this tutorial Select "Edit Path"…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question