Solved

Rotate jpg/tif

Posted on 2004-04-24
7
921 Views
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 http://support.microsoft.com/default.aspx?scid=KB;en-us;q186260 allows a bitmap to be rotated 90 degrees, but it doesn't work for jpgs or tifs.

Many thanks.

0
Comment
Question by:PhillipBurton
  • 2
  • 2
  • 2
7 Comments
 
LVL 13

Expert Comment

by:imarshad
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.......

0
 
LVL 5

Author Comment

by:PhillipBurton
ID: 10911038
If I could use a PictureBox, who would I do the rotation?
0
 
LVL 7

Accepted Solution

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

http://www.pscode.com/vb/scripts/ShowCode.asp?txtCodeId=4583&lngWId=1

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)

...to whatever angle you want (in radians -- i.e. 3.14 / 2 = 90°).

-Burbble
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 13

Assisted Solution

by:imarshad
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
Private Type BITMAPINFO
        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 bmi As BITMAPINFO
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
picRot.Refresh
End Sub
0
 
LVL 5

Author Comment

by:PhillipBurton
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.
0
 
LVL 7

Expert Comment

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

-Burbble
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
In this Micro Tutorial viewers will learn the basic shortcuts and functions in InDesign.
Learn how to download your full Prezi presentation for offline presenting. Prezi doesn’t have to be viewed and shared in a web browser, even with a free account you can download your full presentation to share with others. Be sure to download any vi…

758 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now