473,770 Members | 1,806 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Saving and loading bmp using graphics object

Vin
Hi,

I am using the following code to draw whatever the user draws using
x,y.

// draws lines directly on a winform.
CreateGraphics( ).DrawLine(APen , x, y, OldX, OldY);

Now how do I save the drawing on to a bmp file on my harddisk? C# code
in this regard would be very helpful. I tried all forums but invain.

I tried with Graphics object and then

objBitmap=new Bitmap(400, 200, objGraphics);
objBitmap.Save( @"c:\test.bm p", ImageFormat.Bmp );

which didn't work, leaving me with a blank test.bmp.

Also help me load a bmp from hard disk to this form? C# code please.

Anyone out there who could help me?

Cheers
Vin
Nov 16 '05 #1
5 20292
Hi Vin,

The simplest way might be to create a Bitmap the size of your window and
either draw on both Bitmap and graphics or draw on the bitmap and use
DrawImage to draw the bitmap on the graphics.
This way you can also load the bitmap from a file and use DrawImage to
paste it back to the screen

However, this way won't store any undo information. To do this you would
have to store each line drawn in some array or other, possibly a
GraphicsPath.

Your code won't work because simply using DrawLine doesn't retain
information. If something covers your window or you minimize/maximize it
the drawing will be gone. You might want to read Bob Powell's Faq in this
regard.

http://www.bobpowell.net/faqmain.htm

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
Vin
Thanks Morten,

I understand what you want to say.
But in terms of code I have no clue what you are saying.

A c# code snippet would surely do a world of good to me.
GraphicsPath and all, I am still a novice. Even bob powell's FAQ are a
too elaborative and I got lost somewhere.

Help much appreciated

Thanks
Vin

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #3
First of all, this is not a good way to draw onto the window. You'll find
that if anything causes the window to need repainting (e.g. it is obscured
by some other window, or resized, or minimized, or is moved partially off
the screen and then back on) you'll lose all your drawing.

This is because there is nothing that actually stores what you've drawn onto
the window - you're just drawing onto the screen.

Windows has always relied on the ability of applications to recreate their
appearance on demand. It sends them a message (WM_PAINT) whenever it wants
them to repaint themselves, and in .NET this is manifest through the OnPaint
event in Windows Forms.

So the fact that nothing is actually storing your drawing output should make
it fairly clear that there isn't any method you can call to save the
output...

In fact the output might not even make it onto the screen - if you run the
code below when the window happens to be obscured or minimized, that drawing
won't go anywhere at all, not even onto the screen.

Here's what you should do:

public class MyForm : Form
{
private Bitmap bmp;

... and in whatever function it was that you were going to do your
drawing,
put this code....

bmp = new Bitmap(ClientRe ctangle.Width, ClientRectangle .Height);
using (Graphics g = Graphics.FromIm age(bmp)
{
g.DrawLine(APen , x, y, OldX, OldY);
}

...

protected override void OnPaint(PaintEv entArgs e)
{
e.Graphics.Draw Image(bmp, 0, 0);

base.OnPaint(e) ;
}

}
And now you'll be able to save that image with this code:

bmp.Save("MyBit map.bmp", ImageFormat.Bmp );
What's been done here is that we've created a Bitmap object to hold the
image. If you want to have the image stored somewhere it's your
responsibility to make that happen, so in this example, that's why we create
the Bitmap.

By overriding OnPaint, we make sure that the contents of the Bitmap get
shown on the screen whenever Windows asks us to refresh our application's
appearance.

And because we've stored the drawing inside a Bitmap, we can now save it out
to disk whenever we like using the code shown above.

To load it back from disk, do this:

bmp = new Bitmap("MyBitma p.bmp");

Hope that helps.
--
Ian Griffiths - http://www.interact-sw.co.uk/iangblog/
DevelopMentor - http://www.develop.com/

"Vin" <kg*******@gmai l.com> wrote:> Hi,

I am using the following code to draw whatever the user draws using
x,y.

// draws lines directly on a winform.
CreateGraphics( ).DrawLine(APen , x, y, OldX, OldY);

Now how do I save the drawing on to a bmp file on my harddisk? C# code
in this regard would be very helpful. I tried all forums but invain.

I tried with Graphics object and then

objBitmap=new Bitmap(400, 200, objGraphics);
objBitmap.Save( @"c:\test.bm p", ImageFormat.Bmp );

which didn't work, leaving me with a blank test.bmp.

Also help me load a bmp from hard disk to this form? C# code please.

Anyone out there who could help me?

Cheers
Vin

Nov 16 '05 #4
Vin
Thanks Ian, you've been of great help.
I did what you suggested and it seems to work. But I got couple of
problems.

1> I can't see my lines being drawn on mousemove. When I move the
window, resize it the drawing gets shown. but no on mouse move.

2> When I save the bitmap, the bmp file is black, where as my form
suface is white. No clue what's going wrong here.

Here's my code.

Kindly point me where am I making a mistake.

private Pen APen;
private int OldX;
private int OldY;

private void DrawPoint(int x, int y)
{
using (objGraphics = Graphics.FromIm age(objBitmap))
{
objGraphics.Dra wLine(APen, x, y, OldX, OldY);
}
}

protected override void OnPaint(PaintEv entArgs e)
{
e.Graphics.Draw Image(objBitmap , 0, 0);
base.OnPaint(e) ;
}

private void ResetOffset(int x, int y)
{
ResetOffset(x, y, false);
}

private void ResetOffset(int x, int y, bool Reset)
{
OldX = x + System.Convert. ToInt32(Reset);
OldY = y + System.Convert. ToInt32(Reset);
}

private void Scribbler_Mouse Move(object sender,
System.Windows. Forms.MouseEven tArgs e)
{
StatusBar1.Text = string.Format(" X:{0:d},Y:{0:d} ", e.X, e.Y);
if ((e.Button == MouseButtons.Le ft))
{

DrawPoint(e.X, e.Y);
}
ResetOffset(e.X , e.Y, e.Button == MouseButtons.No ne);
}

private void Scribbler_Mouse Down(object sender,
System.Windows. Forms.MouseEven tArgs e)
{
DrawPoint(e.X, e.Y);
}

private void Button1_Click(o bject sender, System.EventArg s e)
{
APen = new Pen(Color.Red);
}

private void Button2_Click(o bject sender, System.EventArg s e)
{
APen = new Pen(Color.Blue) ;
}

private void Button3_Click(o bject sender, System.EventArg s e)
{
APen = new Pen(Color.Green );
}

private void Button4_Click(o bject sender, System.EventArg s e)
{
APen = new Pen(Color.Black );
}

private void btnSave_Click(o bject sender, System.EventArg s e)
{
objBitmap.Save( @"c:\Save.bm p", ImageFormat.Bmp );
objBitmap.Dispo se();
objGraphics.Dis pose();
}

private void Scribbler_Load( object sender, System.EventArg s e)
{
objBitmap = new Bitmap(ClientRe ctangle.Width,
ClientRectangle .Height);
}

Thanks
Vin
Nov 16 '05 #5
Windows needs to know that part of your window needs redrawing. Remember
that you're now drawing into an off-screen bitmap, and Windows doesn't know
that this bitmap is a copy of what you want to appear on screen. (Windows
has no intrinsic support for this idea of a window with some bitmap backing
store, so it just plain doesn't understand the idea that a bitmap might be
associated with what's on screen.)

So you need to tell Windows. The way to do this is to call the Invalidate
method on your control. You could just call it with no parameters - that
will refresh the whole control. However, that can be a little slow - a more
efficient way of doing it would be to call the one that takes a rectangle,
telling it which part just changed. When you do either of these, your
OnPaint method will get called, allowing the results to appear on screen.

So here's the simple but slow version:

private void DrawPoint(int x, int y)
{
using (objGraphics = Graphics.FromIm age(objBitmap))
{
objGraphics.Dra wLine(APen, x, y, OldX, OldY);
}

Invalidate();
}

I'll leave it as an exercise to work out the faster version. ;-)
Also, you're going to have to think about what you want to do when the
window is resized. Resizing the bitmap is one option, but to do that you'd
have to build a new one, and then draw the existing one into the new one.
Or you could just pick a bitmap size up front and not resize it. (So think
about what Windows PaintBrush does. It doesn't keep resizing the bitmap
every time you resize the window. The bitmap size remains the same until
you change it.)
--
Ian Griffiths - http://www.interact-sw.co.uk/iangblog/
DevelopMentor - http://www.develop.com/

"Vin" wrote:
Thanks Ian, you've been of great help.
I did what you suggested and it seems to work. But I got couple of
problems.

1> I can't see my lines being drawn on mousemove. When I move the
window, resize it the drawing gets shown. but no on mouse move.

2> When I save the bitmap, the bmp file is black, where as my form
suface is white. No clue what's going wrong here.

Here's my code.

Kindly point me where am I making a mistake.

private Pen APen;
private int OldX;
private int OldY;

private void DrawPoint(int x, int y)
{
using (objGraphics = Graphics.FromIm age(objBitmap))
{
objGraphics.Dra wLine(APen, x, y, OldX, OldY);
}
}

protected override void OnPaint(PaintEv entArgs e)
{
e.Graphics.Draw Image(objBitmap , 0, 0);
base.OnPaint(e) ;
}

private void ResetOffset(int x, int y)
{
ResetOffset(x, y, false);
}

private void ResetOffset(int x, int y, bool Reset)
{
OldX = x + System.Convert. ToInt32(Reset);
OldY = y + System.Convert. ToInt32(Reset);
}

private void Scribbler_Mouse Move(object sender,
System.Windows. Forms.MouseEven tArgs e)
{
StatusBar1.Text = string.Format(" X:{0:d},Y:{0:d} ", e.X, e.Y);
if ((e.Button == MouseButtons.Le ft))
{

DrawPoint(e.X, e.Y);
}
ResetOffset(e.X , e.Y, e.Button == MouseButtons.No ne);
}

private void Scribbler_Mouse Down(object sender,
System.Windows. Forms.MouseEven tArgs e)
{
DrawPoint(e.X, e.Y);
}

private void Button1_Click(o bject sender, System.EventArg s e)
{
APen = new Pen(Color.Red);
}

private void Button2_Click(o bject sender, System.EventArg s e)
{
APen = new Pen(Color.Blue) ;
}

private void Button3_Click(o bject sender, System.EventArg s e)
{
APen = new Pen(Color.Green );
}

private void Button4_Click(o bject sender, System.EventArg s e)
{
APen = new Pen(Color.Black );
}

private void btnSave_Click(o bject sender, System.EventArg s e)
{
objBitmap.Save( @"c:\Save.bm p", ImageFormat.Bmp );
objBitmap.Dispo se();
objGraphics.Dis pose();
}

private void Scribbler_Load( object sender, System.EventArg s e)
{
objBitmap = new Bitmap(ClientRe ctangle.Width,
ClientRectangle .Height);
}

Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3646
by: Novice | last post by:
I'm afraid I will incur the wraith of Mr. Powell on this one - but I did read his #1 FAQ and some others and I still can't figure this out. I created this little c# app. and I have a PictureBox in my Form. I load this image from the filesystem into the PictureBox and then I draw random little lines on the image. Then when I minimize and reopen the application the little lines are gone. Is there a way to save my lines in memory and...
5
2544
by: Steve Marshall | last post by:
Hi all, I am converting an app which used a picturebox to draw graphs etc onto, then saved them to a file. I can certainly draw things onto a picturbox in VB.NET, but how do I save them to a file? I've looked at Bitmap objects, which support saving, but can't see a way to grab what I have drawn and make a bitmap from it, or whatever. Anybody done this? Thanks for any suggestions
5
4733
by: TheGanjaMan | last post by:
Hi everyone, I'm trying to write up a simple image stamper application that stamps the Exif date information from the jpegs that I've taken from my digital camera and saves the new file with the date stamped on the lower right part of the picture. (I'm not an advanced programmer so my code may not be 100% efficient - sorry, I'm still learning) Everything works fine until the saving part. I've been able to read the file into a...
4
1891
by: Jake G | last post by:
Hi, I wrote a script to generate links to some pictures that I need to regularly update our site with. Is there a way to write a script that just saves the pics to a directory on my pc so I dont have to right click on every link and do a save picture as...? I can't link directly to the pics from our site because our site is https and the site the pics are on are http so the user gets the security warning box every time a pic loads. ...
6
1995
by: John Kotuby | last post by:
Hi all, I am using a 3rd party program in a VS2005 web project. The tool takes as input a string containing HTML and converts it to RTF. I have been creating a page by dynamically loading UserControls and then sending the page to the browser. Now I want to write the contents of the the page to a string variable, convert that variable to the RTF and send that to the browser and I am stuck on the "simple" part of getting the contents of...
9
4273
by: she_prog | last post by:
Dear All, I need to save the content of a panel to a bitmap. The panel can have many child controls which also need to be saved. The problem would be solved if I could have the panel saved to a Graphics object, which is the same as if I'd need to print it. It'd be easy using Control.DrawToBitmap, but I also need the invisible part of the panel (which is hidden because of scrolling) and DrawToBitmap just takes a screenshot.
2
3759
by: JoeC | last post by:
I have successfully saved and re-loaded binary data but I am having some trouble with some data: I wrote a binary graphics object and it takes data in a BYTE form. Here is the data I have and how I save then load it: static BYTE bits = {0xff,0xff, 0x00,0x00, 0x7f,0xfe, 0x7f,0xfe, 0x7f,0xfe, 0x7f,0xfe, 0x7f,0xfe, 0x7e,0x7e, 0x7e,0x7e, 0x7f,0xfe, 0x7f,0xfe, 0x7f,0xfe,
8
2246
by: vicky87.eie | last post by:
I used a picture box to draw lines and rectangle using its graphics object in paint event. Now i need to save those lines i have drawn and print them. I need to know how to save them. I tried image.save. But i didn't work...
0
9617
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10257
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10099
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9904
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8931
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7456
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.