locked
One Question related to Graphics Object, Thanks RRS feed

  • Question

  • Dear All,

    Currently, I am working on a application which will rotate, zoom in and zoom out a selected picture by using Graphics object. But something weird is the operations cannot be saved, below is the codes.

    Dim g As Graphics = Graphics.FromImage(Image.FromFile("old.tif"))
    g.RotateTransform(90)
    g.Save()
    objImage.Save("new.tif", Imaging.ImageFormat.Tiff)

    The function of codes is load a existing TIF file, rotate 90 , then save to disk, but the output new file is not rotate.

    Can any one help me to figure out what's wrong in the code??

     

    Thanks in advance.

    • Moved by Leo Liu - MSFT Thursday, May 26, 2011 2:15 AM Moved for better support. (From:Visual C# General)
    Wednesday, May 25, 2011 6:35 AM

Answers

  • Hi,
    if you just want to rotate the image, you can use the Bitmaps RotateFlip-Method:
        Dim img As Bitmap = CType(Image.FromFile("old.tif"), Bitmap)
        img.RotateFlip(RotateFlipType.Rotate90FlipNone)
        img.Save("new.tif", Imaging.ImageFormat.Tiff)
        img.Dispose()
    
    In your code, you load a Graphics-object from an image, which is not accessible from your code - you do not assign the image to any variable, so what ever you do with your graphics on the image it will be lost at the end.
    'assign the image to a variable
    Dim objImage as Image =  Image.FromFile("old.tif"))
    Dim g As Graphics = Graphics.FromImage()
    'rotates the graphics context, no action on the image taken
    g.RotateTransform(90)
    'here you'd need to draw the image to see the effect of the graphics-rotation

    objImage.Save("new.tif", Imaging.ImageFormat.Tiff)
    'cleanup
    objImage,Dispose()
    see this section on MSDN Library:
    Regards,
      Thorsten
    • Proposed as answer by Mike Feng Thursday, May 26, 2011 2:26 AM
    • Marked as answer by Mike Feng Thursday, June 2, 2011 10:09 AM
    Wednesday, May 25, 2011 8:08 AM

All replies

  • Hi,
    if you just want to rotate the image, you can use the Bitmaps RotateFlip-Method:
        Dim img As Bitmap = CType(Image.FromFile("old.tif"), Bitmap)
        img.RotateFlip(RotateFlipType.Rotate90FlipNone)
        img.Save("new.tif", Imaging.ImageFormat.Tiff)
        img.Dispose()
    
    In your code, you load a Graphics-object from an image, which is not accessible from your code - you do not assign the image to any variable, so what ever you do with your graphics on the image it will be lost at the end.
    'assign the image to a variable
    Dim objImage as Image =  Image.FromFile("old.tif"))
    Dim g As Graphics = Graphics.FromImage()
    'rotates the graphics context, no action on the image taken
    g.RotateTransform(90)
    'here you'd need to draw the image to see the effect of the graphics-rotation

    objImage.Save("new.tif", Imaging.ImageFormat.Tiff)
    'cleanup
    objImage,Dispose()
    see this section on MSDN Library:
    Regards,
      Thorsten
    • Proposed as answer by Mike Feng Thursday, May 26, 2011 2:26 AM
    • Marked as answer by Mike Feng Thursday, June 2, 2011 10:09 AM
    Wednesday, May 25, 2011 8:08 AM
  • ...

    Dim img As Bitmap = CType(Image.FromFile("old.tif"), Bitmap)
    
    

    you could, of course, use the Bitmaps-Constructor here...

    Dim img As New Bitmap([PATH_TO_FILE])
    
    

    Regards,

      Thorsten 


    Wednesday, May 25, 2011 8:52 AM
  • Dear All,

    Currently, I am working on a application which will rotate, zoom in and zoom out a selected picture by using Graphics object. But something weird is the operations cannot be saved, below is the codes.

    Dim g As Graphics = Graphics.FromImage(Image.FromFile("old.tif"))
    g.RotateTransform(90)
    g.Save()
    objImage.Save("new.tif", Imaging.ImageFormat.Tiff)

    The function of codes is load a existing TIF file, rotate 90 , then save to disk, but the output new file is not rotate.

    Can any one help me to figure out what's wrong in the code??

     

    Thanks in advance.


    Total confusion!  You concept of the Graphics class is way off.

    Spend a few moments at www.bobpowell.net

    Wednesday, May 25, 2011 6:02 PM