By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,986 Members | 1,960 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,986 IT Pros & Developers. It's quick & easy.

Question about bitmap

P: n/a
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

Apr 24 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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]
- mv*@spam.guard.caspershouse.com

"poifull" <po*******@yahoo.comwrote in message
news:46***********************@roadrunner.com...
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

Apr 24 '07 #2

P: n/a
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:eX**************@TK2MSFTNGP03.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"poifull" <po*******@yahoo.comwrote in message
news:46***********************@roadrunner.com...
>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


Apr 24 '07 #3

P: n/a
"poifull" <po*******@yahoo.comwrote in message
news:46***********************@roadrunner.com...
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

Apr 24 '07 #4

P: n/a
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" <po*******@yahoo.comwrote in message
news:46***********************@roadrunner.com...
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:eX**************@TK2MSFTNGP03.phx.gbl...
>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]
- mv*@spam.guard.caspershouse.com

"poifull" <po*******@yahoo.comwrote in message
news:46***********************@roadrunner.com.. .
>>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



Apr 24 '07 #5

P: n/a
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:eD**************@TK2MSFTNGP02.phx.gbl...
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" <po*******@yahoo.comwrote in message
news:46***********************@roadrunner.com...
>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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:eX**************@TK2MSFTNGP03.phx.gbl...
>>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]
- mv*@spam.guard.caspershouse.com

"poifull" <po*******@yahoo.comwrote in message
news:46***********************@roadrunner.com. ..
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





Apr 24 '07 #6

P: n/a
"poifull" <po*******@yahoo.comwrote in message
news:46***********************@roadrunner.com...
[...]
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

Apr 24 '07 #7

P: n/a
You are right. It works when I set the Position = 0
Thanks!

"Peter Duniho" <Np*********@NnOwSlPiAnMk.comwrote in message
news:13*************@corp.supernews.com...
"poifull" <po*******@yahoo.comwrote in message
news:46***********************@roadrunner.com...
>[...]
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

Apr 24 '07 #8

P: n/a
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]
- mv*@spam.guard.caspershouse.com

"Peter Duniho" <Np*********@NnOwSlPiAnMk.comwrote in message
news:13*************@corp.supernews.com...
"poifull" <po*******@yahoo.comwrote in message
news:46***********************@roadrunner.com...
>[...]
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

Apr 24 '07 #9

P: n/a
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:Ol**************@TK2MSFTNGP02.phx.gbl...
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

Apr 24 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.