473,326 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Convert, Rotate And Convert Again Error

Tyler Wiebe
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
Expand|Select|Wrap|Line Numbers
  1.         public static System.Drawing.Bitmap ToBitmap(System.Drawing.Icon Icon)
  2.         {
  3.             System.IO.MemoryStream IconStream = new System.IO.MemoryStream();
  4.             Icon.Save(IconStream);
  5.  
  6.             byte[] BitmapBytes = IconStream.ToArray();
  7.  
  8.             System.IO.MemoryStream BitmapStream = new System.IO.MemoryStream(BitmapBytes, 0, BitmapBytes.Length);
  9.             BitmapStream.Write(BitmapBytes, 0, BitmapBytes.Length);
  10.  
  11.             return new System.Drawing.Bitmap(System.Drawing.Bitmap.FromStream(BitmapStream));
  12.         }
  13.  
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.
Expand|Select|Wrap|Line Numbers
  1.         public static System.Drawing.Icon ToIcon(System.Drawing.Bitmap Bitmap)
  2.         {
  3.             return System.Drawing.Icon.FromHandle(Bitmap.GetHicon());
  4.         }
  5.  
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.
Apr 22 '12 #1
2 2190
RhysW
70
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!

Expand|Select|Wrap|Line Numbers
  1. public static Image RotateImage(Image img, float rotationAngle)
  2. {
  3.     //create an empty Bitmap image
  4.     Bitmap bmp = new Bitmap(img.Width, img.Height);
  5.     //turn the Bitmap into a Graphics object
  6.     Graphics gfx = Graphics.FromImage(bmp);
  7.     //now we set the rotation point to the center of our image
  8.     gfx.TranslateTransform((float)bmp.Width / 2, (float)bmp.Height / 2);
  9.     //now rotate the image
  10.     gfx.RotateTransform(rotationAngle);
  11.     gfx.TranslateTransform(-(float)bmp.Width / 2, -(float)bmp.Height / 2);
  12.     //set the InterpolationMode to HighQualityBicubic so to ensure a high
  13.     //quality image once it is transformed to the specified size
  14.     gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
  15.     //now draw our new image onto the graphics object
  16.     gfx.DrawImage(img, new Point(0, 0));
  17.     //dispose of our Graphics object
  18.     gfx.Dispose();
  19.     //return the image
  20.     return bmp;
  21. }
  22.  
Apr 24 '12 #2
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.
Apr 24 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Bartosz Majewski | last post by:
Hello, I have a question. Is there any possibility to compile MS Access database (e.g. *.mdb file) to standalone, executable application? I'd like to use such database application on a computer...
7
by: Jos De Laender | last post by:
Following simple program : #include <stdio.h> char *Convert(int Arg) { static char Buffer; printf("Convert called for %d\n",Arg); sprintf(Buffer,"%d",Arg); printf("Convert about returning...
1
by: Andreas Klemt | last post by:
Hello, I have this session("myNumber") = 888 Dim intNumber As Integer a) intNumber = session("myNumber") b) intNumber = Convert.ToInt32(session("myNumber"))
2
by: Alex Nitulescu | last post by:
Hi. I have created a web-based "file manager", for remote-administration of a web-site. It works okay. The "main" form in the "file manager" is BrowseFiles.aspx. I can edit the text files (among...
3
by: byrd48 | last post by:
Hi, I am developing a web site which allows users to upload and share photos. I have a datalist which lists the photos and has the usual edit, update commands. Within the edititemtemplate, I...
4
by: Mike Collins | last post by:
This worked in the command window while in debug mode ?table.Rows.ItemArray.ToString() "f0165f94-648f-4997-b578-11d89c8b1f61" But gives the error below when I compile. Cannot implicitly...
7
by: WT | last post by:
Hello, VS2005 with SP1. I succefully coverted my Web Site Project to Web Application project, but, for an aspx class, the manuel (by right clic menu) 'Covert to Web Application, gives the...
0
by: IanJSparks | last post by:
I've been doing some image rotation with PIL and comparing the images with imagemagick's convert -rotate output. Imagemagick uses an RBS / Paeth rotation algorithm that appears to give better...
0
by: kronus | last post by:
Hi everyone, Please tell me what I am doing wrong: I have an xml data coming to my Air application that looks something like this: <project name="Sales Demos" path="/Company Home/Client...
5
by: furqi | last post by:
hi every body i write a code in c sharp in which i have made a base class and make an event there.Then i make a derived class and made an other event in that class. Now what i wanna do is that i...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.