Convert, Rotate And Convert Again Error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tyler Wiebe
    New Member
    • Mar 2011
    • 66

    Convert, Rotate And Convert Again Error

    I'm trying to rotate a System.Drawing. Icon, but first I need to convert it to a System.Drawing. Bitmap, then rotate it, and then convert it back to a System.Drawing. Icon, which is being looped, which works fine... for 2:34 minutes.

    I'm using this to convert the System.Drawing. Icon to a System.Drawing. Bitmap
    Code:
            public static System.Drawing.Bitmap ToBitmap(System.Drawing.Icon Icon)
            {
                System.IO.MemoryStream IconStream = new System.IO.MemoryStream();
                Icon.Save(IconStream);
    
                byte[] BitmapBytes = IconStream.ToArray();
    
                System.IO.MemoryStream BitmapStream = new System.IO.MemoryStream(BitmapBytes, 0, BitmapBytes.Length);
                BitmapStream.Write(BitmapBytes, 0, BitmapBytes.Length);
    
                return new System.Drawing.Bitmap(System.Drawing.Bitmap.FromStream(BitmapStream));
            }
    The reason I'm doing this is because for some reason, Icon.ToBitmap() , turns all transparency to white, my method how ever does not.

    But my method also causes an error after 2:34 minutes, which is on the return, which says, "Parameter is not valid."

    And besides that, from what I'm aware, there's no other way of doing this, and this also causes another error after 2:34 minutes.
    Code:
            public static System.Drawing.Icon ToIcon(System.Drawing.Bitmap Bitmap)
            {
                return System.Drawing.Icon.FromHandle(Bitmap.GetHicon());
            }
    After 2:34 minutes, I also get the error, "A generic error occurred in GDI+."

    Most of the time the GDI+ error occurs, but sometimes the other one occurs, and I'm not sure what to do about this.

    Any help would really be appreciated.
  • RhysW
    New Member
    • Mar 2012
    • 70

    #2
    http://www.dreamincode.net/forums/to...space-part-ii/ this has got everything and more that you could ever want to know about the system.drawing namespace, complete with demonstrations, for example here is the code used to rotate an image, it is different from yours and therefore may give you different results, (it still requires you to turn it into a bitmap and back but you seem to be fine with doing that.) let me know if this code works better for you!

    Code:
    public static Image RotateImage(Image img, float rotationAngle)
    {
        //create an empty Bitmap image
        Bitmap bmp = new Bitmap(img.Width, img.Height);
        //turn the Bitmap into a Graphics object
        Graphics gfx = Graphics.FromImage(bmp);
        //now we set the rotation point to the center of our image
        gfx.TranslateTransform((float)bmp.Width / 2, (float)bmp.Height / 2);
        //now rotate the image
        gfx.RotateTransform(rotationAngle);
        gfx.TranslateTransform(-(float)bmp.Width / 2, -(float)bmp.Height / 2);
        //set the InterpolationMode to HighQualityBicubic so to ensure a high
        //quality image once it is transformed to the specified size
        gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
        //now draw our new image onto the graphics object
        gfx.DrawImage(img, new Point(0, 0));
        //dispose of our Graphics object
        gfx.Dispose();
        //return the image
        return bmp;
    }

    Comment

    • Tyler Wiebe
      New Member
      • Mar 2011
      • 66

      #3
      I should have mentioned that the code I'm using in to rotate the image between conversion is exactly that.

      I'm having no problem converting the icon to a bitmap, rotating it 15 degrees, and then converting it back to an icon every 40 milliseconds for 2:34 minutes, but after that I get one of those two errors, which from the fact that I've rotated it many times during a time span of 2:34 minutes, I don't understand what is causing this error.

      Comment

      Working...