poifull,
I made a mistake, you should really use the RotateFlip method on the
Bitmap class in order to do what you want, like so:
using (Bitmap bmp = new Bitmap(100, 50))
using (Graphics g = Graphics.FromImage(bmp))
using (SolidBrush brush = new SolidBrush(Color.Red))
using (Font f = new Font("Arial", 9.0F))
{
brush.Color = Color.White;
g.FillRectangle(brush, new Rectangle(0, 0, 100, 50));
brush.Color = Color.Red;
g.DrawString("Test", f, brush, new PointF(0.0F, 0.0F));
bmp.RotateFlip(RotateFlipType.Rotate270FlipNone);
bmp.Save(@"C:\temp\test.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
}
The RotateFlip method is a much easier way of doing what you want.
Also, use using statements, so you can guarantee cleanup in your program.
Hope this helps.
poifull said:
Thanks for your help. I don't know why I can't get the image to rotate.
Here is the code:
Bitmap bmp = new Bitmap(100, 50);
Graphics g = Graphics.FromImage(bmp);
SolidBrush brush = new SolidBrush(Color.Red);
Font f = new Font("Arial", 9.0F);
brush.Color = Color.White;
g.FillRectangle(brush, new Rectangle(0, 0, 100, 50));
brush.Color = Color.Red;
g.DrawString("Test", f, brush, new PointF(0.0F, 0.0F));
g.RotateTransform(270.0F);
g.Save();
bmp.Save(@"C:\temp\test.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
g.Dispose();
brush.Dispose();
bmp.Dispose();
Thanks again.
Nicholas Paldino said:
poifull,
Well, you will need the bitmap class to create the bitmap. Once you
have that, you can call the static FromImage method on the Graphics
class to get a Graphics instance you can use to draw on the bitmap.
Once you use the methods on the Graphics instance to draw on the
bitmap.
Then, you can call the RotateTransform method on the Graphics
instance to rotate the image.
Finally, call the Save method on the Bitmap to save to a stream.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
Hi All,
I have the following task:
1. create an image object in memory (a rectangle with nothing on it)
2. write some text on the rectangle
3. rotate the image 90 degree
4. save the image to a stream
Can anyone tell me the steps I need to perform and the classes/methods
involved? You don't have to write out the whole program. Some basic
ideas will be enough.
Thanks for your help,
poifull