Important alert: (current site time 7/16/2013 4:11:14 AM EDT)
 

VB icon

Picture Commands

Email
Submitted on: 4/22/1999
By: Michael L. Canejo  
Level: Advanced
User Rating: By 1 Users
Compatibility: VB 4.0 (32-bit), VB 5.0, VB 6.0
Views: 27695
author picture
(About the author)
 
     This includes the source code for Rotating a Picture 45 Degrees, Flipping a Picture Horizontally, and Flipping a Picture Vertically! Very usefull if your working on your own picture editing program.
Questions: mikecanejo@Hotmail.com
 

Windows API/Global Declarations:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'**************************************
'Windows API/Global Declarations for :Picture Commands
'**************************************
'Copy and Paste the following into the New Module(Module1.Bas)..not in the form!
Global Const SRCCOPY = &HCC0020
Global Const Pi = 3.14159265359
Declare Function SetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long
Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Declare Function StretchBlt Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long
Sub bmp_rotate(pic1 As PictureBox, pic2 As PictureBox, ByVal theta!)
 'Rotate the image in a picture box.
 'pic1 is the picture box with the bitmap to rotate
 'pic2 is the picture box to receive the rotated bitmap
 'theta is the angle of rotation
 
 Dim c1x As Integer, c1y As Integer
 Dim c2x As Integer, c2y As Integer
 Dim a As Single
 Dim p1x As Integer, p1y As Integer
 Dim p2x As Integer, p2y As Integer
 Dim n As Integer, r As Integer
 
 c1x = pic1.ScaleWidth \ 2
 c1y = pic1.ScaleHeight \ 2
 c2x = pic2.ScaleWidth \ 2
 c2y = pic2.ScaleHeight \ 2
 If c2x < c2y Then n = c2y Else n = c2x
 n = n - 1
 pic1hDC% = pic1.hdc
 pic2hDC% = pic2.hdc
 For p2x = 0 To n
 For p2y = 0 To n
 If p2x = 0 Then a = Pi / 2 Else a = Atn(p2y / p2x)
 r = Sqr(1& * p2x * p2x + 1& * p2y * p2y)
 p1x = r * Cos(a + theta!)
 p1y = r * Sin(a + theta!)
 c0& = GetPixel(pic1hDC%, c1x + p1x, c1y + p1y)
 c1& = GetPixel(pic1hDC%, c1x - p1x, c1y - p1y)
 c2& = GetPixel(pic1hDC%, c1x + p1y, c1y - p1x)
 c3& = GetPixel(pic1hDC%, c1x - p1y, c1y + p1x)
 If c0& <> -1 Then xret& = SetPixel(pic2hDC%, c2x + p2x, c2y + p2y, c0&)
 If c1& <> -1 Then xret& = SetPixel(pic2hDC%, c2x - p2x, c2y - p2y, c1&)
 If c2& <> -1 Then xret& = SetPixel(pic2hDC%, c2x + p2y, c2y - p2x, c2&)
 If c3& <> -1 Then xret& = SetPixel(pic2hDC%, c2x - p2y, c2y + p2x, c3&)
 Next
 
 t% = DoEvents()
 Next
End Sub
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: Picture Commands
' Description:This includes the source code for Rotating a Picture 45 Degrees, Flipping a Picture Horizontally, and Flipping a Picture Vertically! Very usefull if your working on your own picture editing program. <br>Questions: mikecanejo@Hotmail.com
' By: Michael L. Canejo
'
' Assumes:'Please follow these simple steps. DONT SKIP ANYTHING!
'1.) Make a New Project in your 32bit Visual Basic
'2.) Add a New Module(Module1.Bas) to the New Project
'3.) Add a PictureBox to the Form(Picture1)and Insert a Picture in the 'PictureBox
'4.) Add a PictureBox to the Form(Picture2)and keep it blank
'5.) Add a CommandButton to the Form(Command1)
'6.) Add a CommandButton to the Form(Command2)
'7.) Add a CommandButton to the Form(Command3)
'Thats it, hope you followed EVERY step.
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1606&lngWId=1'for details.'**************************************

'Ok, now just Copy and Paste everything here into the Form1..not in the Bas!
'<Start Copying>
Private Sub Command1_Click()
 'flip horizontal
 Picture2.Cls
 px% = Picture1.ScaleWidth
 py% = Picture1.ScaleHeight
 retval% = StretchBlt(Picture2.hdc, px%, 0, -px%, py%, Picture1.hdc, 0, 0, px%, py%, SRCCOPY)
End Sub
Private Sub Command2_Click()
 'flip vertical
 Picture2.Cls
 px% = Picture1.ScaleWidth
 py% = Picture1.ScaleHeight
 retval% = StretchBlt(Picture2.hdc, 0, py%, px%, -py%, Picture1.hdc, 0, 0, px%, py%, SRCCOPY)
End Sub
Private Sub Command3_Click()
 'rotate 45 degrees
 Picture2.Cls
 Call bmp_rotate(Picture1, Picture2, 3.14 / 4)
End Sub
Private Sub Form_Load()
Command1.Caption = "Flip Horizontal"
Command2.Caption = "Flip Vertical"
Command3.Caption = "Rotate 45 Degrees"
 Picture1.ScaleMode = 3
 Picture2.ScaleMode = 3
End Sub
'<Stop Copying...END>


Other 45 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
5/2/1999 5:04:00 PMHenry Franklin

Hey cool code Mike. Im working on
making my own Picture Developement
software and this is a big help.
Who knwos, ill probobly make an
program just like ADOBE :-)
(If this comment was disrespectful, please report it.)

 
5/3/1999 2:07:00 PMgary noell

I am using this code to invert images. I would like to save the inverted images to a new directory. Can you help?
(If this comment was disrespectful, please report it.)

 
5/4/1999 4:03:00 PMYura Korneev

Thanks! I maked "Diffuse" effect while
learned from your example. Now i know and
understand how to make a lot of effects.
Good source, keep it that way.
(If this comment was disrespectful, please report it.)

 
5/6/1999 7:52:00 AMNAM

You Should have demonstrations so we could view your code and understand it here!!
(If this comment was disrespectful, please report it.)

 
5/6/1999 8:25:00 AMAdnan Yousuf

Hay this code really works.. It gave alot of help in the development of my own Picture editor
(If this comment was disrespectful, please report it.)

 
5/9/1999 4:23:00 PMMinhaj Arifin

Dear Sir,
I thank you for your generosity,kindly inform me how it is possible to move controls across a form in slow motion.
Minhaj Arifin

(If this comment was disrespectful, please report it.)

 
5/10/1999 1:13:00 AMDavid

I'm new to vb5 and im trying to make my pictures show so they take and show 1 image for 5 seconds then goto another how can i do this if you could help me thanks you And if anyone has a bas for excite chat or icq could you send please want to make some personal affects to it thanks
(If this comment was disrespectful, please report it.)

 
5/10/1999 3:29:00 AMPartono

I want to develop a security system for my application built by VB 6.0. The security I want is that can be detect the user when login and then grayed some menu that user can't access. If you have source code for helping me, I'll be very grateful. Thanks.
(If this comment was disrespectful, please report it.)

 
5/12/1999 4:57:00 AMuser

This is depressing. using setpixel is so long on big picutres. And using setpixel is chince. Tell me when you can do it a real way
(If this comment was disrespectful, please report it.)

 
5/19/1999 5:51:00 PMDiamante

Nice code !!! But, tell me, how can i save the DC's bitmap in my HD ??
(If this comment was disrespectful, please report it.)

 
5/22/1999 10:22:00 AMVbmaster

About SAVING the result picture, try setting [Picturebox].Picture = [Picturebox].Image, and then u will be able to save the picture with SavePicture method.. i think
(If this comment was disrespectful, please report it.)

 
5/23/1999 5:07:00 AMdharma

hey the code is is real cool.Some one who can help alittle .
.How can you separate the
primary colors from a given color.
please with a little lucid explanation.
Thanx a lot.
(If this comment was disrespectful, please report it.)

 
5/27/1999 6:13:00 PMHarry Noche

You have very helpful codes. Maybe
you can help us develop an imaging
software capable of accessing the
database like SQL, Oracle and JASMINE,
and scanned picture files.
What's more interesting is that it must
have some character recognition, able to
correct some invalid characters in the
picture. I hope we could here from you
very soon. Thanks and more power!
(If this comment was disrespectful, please report it.)

 
5/28/1999 2:28:00 PMSz Levente

My question is:
How to use asm commands in vb 5.0 ?
If you have an answer, please send my also.
Thanks'
(If this comment was disrespectful, please report it.)

 
5/30/1999 6:33:00 AMYura Korneev

Yes code little slow, but using it i developed
my own image format. Now i'm working on
analyzing colors in image and programing compression alghoritm
same as used in JPEG images.
(If this comment was disrespectful, please report it.)

 
8/1/1999 11:44:00 AMGnorran

How to save pictures you have manipulated with
different API's or functions ?

Simply do:

SavePicture Picture1.Image,"C:\Image.bmp"

Notice the .IMAGE ?
set autoredraw = true and it should work fine...

// Gnorran
(If this comment was disrespectful, please report it.)

 
8/18/1999 5:29:00 AMvbmaniac

T H A N K y o u Gnorran!!!!!!!!!!!!!!!!!!!!!!!!!!!!
but i have a question :How can i import scanned pictures from my scanner]
again thank you
(If this comment was disrespectful, please report it.)

 
9/12/1999 6:17:00 PMFinog (Master Programmer?)

Nifty code, it sure beats using the point and reverse methods.
(If this comment was disrespectful, please report it.)

 
10/5/1999 12:46:00 PMRyan

Sheesh! It just seems like all you people come here and post a message for someone to help you!! Try figuring it out on your own (VB Help files are a start)... if you have, maybe try adding some grammar and punctuation to your message and tell us you have tried. I don't like helping people that won't even try. We have lives too you know! :) By the way... nice code! My way was much worse!
(If this comment was disrespectful, please report it.)

 
11/24/1999 4:35:00 PMSteve

Is there a way to do this using the KeyDown Event?

(If this comment was disrespectful, please report it.)

 
3/15/2000 4:54:29 AMsmiffe927563

nice code when you hit command button 3 it angles the picture ok but the picture box cuts off the corners of the picture. To solve this i added these two lines to command3_click()

Picture2.Width = Sqr((Picture1.Width * Picture1.Width) + (Picture1.Height * Picture1.Height))

Picture2.Height = ((Sin(3.14 / 4) * Picture1.Height) + (Sin(3.14) * Picture1.Width))

I added these two lines before the bmp_rotate call and it seems to work

if anybody knows a better way to do this pleas let me know
(If this comment was disrespectful, please report it.)

 
10/13/2002 11:19:55 AM

what code i had to put in the Moudle file so the rarte program can work !?
(If this comment was disrespectful, please report it.)

 
10/13/2002 11:20:05 AM

what code i had to put in the Moudle file so the rotate program can work !?
(If this comment was disrespectful, please report it.)

 
4/16/2004 2:16:37 AM

Thank you, however on the 45 I get an overflow error - Debug shows it to be the line:
pic1hDC% = Pic1.hdc
Any ideas what may be wrong? - Thanks in advance.
(If this comment was disrespectful, please report it.)

 
6/28/2005 2:47:19 PMAbraham Saputra

nice code. very usefull. thanks
run-time error '6': overflow :
do change the "pic1hDC" with "pic1.hdc
it will solve that problem.

thanks

(If this comment was disrespectful, please report it.)

 
9/8/2005 3:03:16 AMC.Naguib

Thank you, change pic1hDC%(integer) with pic1hDC&(Long) and pic2hDC&
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.