Solved

Rotate jpg/tif

Posted on 2004-04-24
7
928 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Many programs have tried to outwit PowerPoint in terms of technology and skill. These programs, however, still lack several characteristics that PowerPoint has possessed from the start. Here's why PowerPoint replacements won't entirely work for desi…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
In this Micro Tutorial viewers will learn the basic shortcuts and functions in InDesign.

809 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