473,408 Members | 2,025 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,408 software developers and data experts.

Question about bitmap

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
9 1900
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
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
"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
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
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
"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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: LG | last post by:
Just have a question with regards to the clipboard, and how to read what other applications (Adobe InDesignCS) place in the clipboard. I am currently in the process of creating a booklet from a...
4
by: Darren Clark | last post by:
I am trying to simply embed a opaque image over another image... if i use a windows form it works.. e.g. Graphics g = this.pictureBox1.CreateGraphics(); g.Clear(this.BackColor);
3
by: news | last post by:
Hi Guys, Sorry if this is an obvious question but Im trying to rotate a bitmap and then save it. So far Ive only found how to rotates a Graphic but I can't find how to save it. I also want to...
2
by: Michael Murphy | last post by:
At the risk of being publically humiliated, might I ask the following simple question. I have a solution with one project. This project, WindowsApplication1, has one "form" and one "bitmap" (the...
2
by: Colin McGuire | last post by:
Sorry to ask a simple question on syntax but the line inside the subroutine "assignBitmapArray" won't compile giving me an "Expression Expected" error. What is the correct syntax to initialize an...
7
by: hamil | last post by:
The following code will display a tif file on a form. When another form is moved over the tif image, the tif image is erased where the form was moved. A paint event occurs when this happens. My...
6
by: **Developer** | last post by:
usually I'd do: Drawing.Image.FromFile( I noticed I once did without thinking: Drawing.Bitmap.FromFile( I assumed this worked because Bitmap inherits from Image, but for fun I thought I'd...
9
by: poifull | last post by:
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...
5
by: Andy | last post by:
Hi, I am using the following code to render a text string in a new bitmap file. The code works, but the text looks, well, crappy, even though I told it to use ClearType hints. Any idea how to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.