Question about bitmap

  • Thread starter Thread starter poifull
  • Start date Start date
P

poifull

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
 
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.
 
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)

poifull said:
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
 
poifull said:
Thanks for your help. I don't know why I can't get the image to rotate.

You need to set the transform *before* drawing whatever it is you want.

The RotateTransform() method (and the other Graphics class transformation
methods) set the transform used in the Graphics object when things are
drawn. It's only applied when things are drawn.

So, you need to set the transform first, then draw the stuff you want
rotated: the bitmap, the rectangle, and the text. Note that this means you
can't just start with a Graphics obtained from the Bitmap object. You need
a new Bitmap object with appropriate dimensions (so in this case, with the
width and height transposed from the original Bitmap), into which you draw
all the stuff, including the original Bitmap.

Pete
 
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)

poifull said:
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
 
Hi. I'm running into another problem. Instead of saving the BMP to a file,
I'm trying to save it to byte array:

bmp.RotateFlip(RotateFlipType.Rotate270FlipNone);
// bmp.Save(@"C:\temp\test.bmp", System.Drawing.Imaging.ImageFormat.Bmp);

System.IO.MemoryStream stream = new System.IO.MemoryStream();

bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);

byte[] b = new byte[stream.Length];

stream.Read(b, 0, b.Length);


At the end of this code block, b[].length = 20054 but it only has zero's in
it. The output needs to be a byte array.

Thanks again to those who responded.


Nicholas Paldino said:
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
 
poifull said:
[...]
bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);

byte[] b = new byte[stream.Length];

stream.Read(b, 0, b.Length);


At the end of this code block, b[].length = 20054 but it only has zero's
in it. The output needs to be a byte array.

What is the return value for the call to the Stream.Read() method?

My guess is that it's zero, and my guess is that it's zero because you
didn't reset the stream position after writing to the stream (note that the
second parameter indicates where in the byte array to write the data, and it
doesn't affect where from the stream the data is read...the stream's current
position controls that).

I might be wrong...I didn't try compiling your code and testing it to see
for myself. But that seems like the most likely explanation to me. :)

Pete
 
You are right. It works when I set the Position = 0
Thanks!

Peter Duniho said:
poifull said:
[...]
bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);

byte[] b = new byte[stream.Length];

stream.Read(b, 0, b.Length);


At the end of this code block, b[].length = 20054 but it only has zero's
in it. The output needs to be a byte array.

What is the return value for the call to the Stream.Read() method?

My guess is that it's zero, and my guess is that it's zero because you
didn't reset the stream position after writing to the stream (note that
the second parameter indicates where in the byte array to write the data,
and it doesn't affect where from the stream the data is read...the
stream's current position controls that).

I might be wrong...I didn't try compiling your code and testing it to see
for myself. But that seems like the most likely explanation to me. :)

Pete
 
Of course, the easier thing to do would have been to call the ToArray
method on the MemoryStream instance to get the byte array. =)

The reason why the call to Read fails is that the pointer in the stream
is at the end of the stream after the write of the bitmap to the stream.
You then try and perform a read on a stream that has nothing else to read.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Peter Duniho said:
poifull said:
[...]
bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);

byte[] b = new byte[stream.Length];

stream.Read(b, 0, b.Length);


At the end of this code block, b[].length = 20054 but it only has zero's
in it. The output needs to be a byte array.

What is the return value for the call to the Stream.Read() method?

My guess is that it's zero, and my guess is that it's zero because you
didn't reset the stream position after writing to the stream (note that
the second parameter indicates where in the byte array to write the data,
and it doesn't affect where from the stream the data is read...the
stream's current position controls that).

I might be wrong...I didn't try compiling your code and testing it to see
for myself. But that seems like the most likely explanation to me. :)

Pete
 
Nicholas Paldino said:
Of course, the easier thing to do would have been to call the ToArray
method on the MemoryStream instance to get the byte array. =)

I suppose. Though you hardly save any code, and it only works if you know
in advance that you have a MemoryStream (which is the case here, but not as
useful for illustrative purposes).
The reason why the call to Read fails is that the pointer in the stream
is at the end of the stream after the write of the bitmap to the stream.
You then try and perform a read on a stream that has nothing else to read.

I thought that's what I said. :)

Pete
 
Back
Top