Rotate an image

Is it possible to rotate a picturebox or an image inside a picturebox

Also, is there a way to make the picturebox backcolor actually transparent Setting it to Web/Transparent does not work.



Answer this question

Rotate an image

  • Chris Sells

    nogChoco: That's the way to rotate an image. There's usually a simple way to do just about anything in .Net.

    Now can you explain the Transform Matrix


  • James Emydex

    Net 3.0

    Edit:

    If you want to rotate by degree, use Net 3.0. 


  • mrtn

    In your paint event, do a Graphics.RotateTransform(Angle) and then whatever you paint next, will be at that angle.

  • Gregg Boer MSFT

    Here you go Troy.

    'rotate an image

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim RotatedImage As Image
    RotatedImage = PictureBox1.Image
    RotatedImage.RotateFlip(RotateFlipType.Rotate90FlipNone)
    PictureBox1.Image = RotatedImage
    End Sub

    'Call MakeTransparent() with a color to set it as transparent

    'or call it w/o any parameters to let windows forms choose a color

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    Dim bmp As Bitmap

    bmp = PictureBox1.Image

    bmp.MakeTransparent(System.Drawing.Color.Black)

    PictureBox1.Image = bmp

    End Sub

    'here's an example that lets you choose which color on the bitmap

    'to make transparent when the user clicks on the picturebox

    Private Sub PictureBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseClick

    Dim bmp As Bitmap

    Dim TransparentColor As System.Drawing.Color

    bmp = PictureBox1.Image

    TransparentColor = bmp.GetPixel(e.X, e.Y)

    bmp.MakeTransparent(TransparentColor)

    PictureBox1.Image = bmp

    End Sub

    Hope this helps,
    Adam Braden
    Visual Basic Team


  • wyx2000

    Lectronx & John - the code works in Visual Studio 2005 and .Net 2.0.

    Adam


  • Yunwen Bai

    Hello

    After pasting in the above code for rotating a picturebox image, I get an error 'RotateFlip is not a member of System.Drawing.Image'

    I thought it would be a cakewalk after reading Troy's response.

    The code for the form reads:

    Imports System.Drawing

    Public Class POD1
    Private bitmap1 As Bitmap

    Private Sub KNOB1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KNOB1.Click
    Dim RotatedImage As Image
    RotatedImage =KNOB1.Image
    RotatedImage.RotateFlip(RotateFlipType.Rotate90FlipNone)
    KNOB1.Image = RotatedImage
    End Sub

    End Class


  • rodrb

    Adam:

    I don't see a degree option in your code. How do I rotate by 14.3 degrees


  • Ignez

    Most of the rotate options in .NET specify 90 degree increments, I don't know how to do it in otherwise. I'd try the windows forms forum.

    Hope this helps,

    Adam

    Visual Basic Team


  • codingIntern

    I just love it when people know what they are talking about. And examples too. Just wonderful.

    BTW, my images are Portable Network Graphics with a transparent color already saved in it. Is there still a way to do it or would I have to convert all my images to non-transparent. When I say transparent I mean the transparency of the image itself not of the picturebox.

    I think I may have just confused you. And myself.

    Edit: Another question that deals with images: What is the difference between ImageIndex and ImageKey They both seem to do the same thing.


  • goke

    The matrix is just a way to bundle the coordinate-offsets, but you don't really need a matrix to do the moving/rotating/scaling if you use the TranslateTransform to move, RotateTransform to rotate and ScaleTransform to scale. I'm gonna give it a shot - should be a nice control to have.


  • Donaghy

    Ok, thanks for your help.

    About the imageIndex and ImageKey: that was the only difference I saw also. The index number the images in my imagelist like an array and the key just gave the images in my imagelist the name of the image.



  • D. Swicegood

    I would think that if the .PNG already had transparent information in it, that it would render correctly in the picturebox. At this point you are at the limits of my expertise in this area. I recommend you post this specific question to the Windows Form section - http://forums.microsoft.com/MSDN/ShowForum.aspx ForumID=8&SiteID=1

    Regarding ImageIndex vs. ImageKey - they are used for the same purpose, to reference into the ImageList control. However the ImageIndex uses and integer like 0, 1, 2, etc. while key can use a string like "ImageBlue", "Mountains", "Cars" or whatever you want to name that image.

    Good Luck!

    -Adam


  • Rotate an image