472,976 Members | 1,637 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,976 software developers and data experts.

How to save to a bitmap?

Lee
I have the following code
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Collections;
namespace FINN
{

public class flickerFreePictureBox : PictureBox
{
public ArrayList Lines = new ArrayList();
public flickerFreePictureBox()
{

this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle (ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
//this.MouseMove += new MouseEventHandler(_MouseMove);
}
protected override void OnPaint(PaintEventArgs e)
{
bool error = false;
//e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
try
{
foreach(scribbleObject al in Lines)
{
bool Drawdot = false;
try
{
Point[] linelist = new Point[
al.linePositions.Count];

al.linePositions.CopyTo(0,linelist,0,al.linePositi ons.Count);
e.Graphics.DrawLines(al.pen(), linelist);
}
catch
{
Drawdot = true; //we want to draw a dot
}
if(Drawdot)
{
//Draw a single dot as the user has only
clicked, rather than clicked and dragged
//TODO: Draw a dot
}

}
}
catch
{
error = true;
}
if(error)
{
//this.OnPaint(e);
}
}
}

}

And I would like to be able to create a bitmap of what gets drawn in
OnPaint so that I can save it to file, I've tried a few things, but so
far just ended up with black bitmaps the same size as the control that
code creates, any suggestions or code snippets would be a great help.

Cheers

Lee

Oct 24 '06 #1
12 3974
Hi Lee

Before Bob Powell notices you are using a PictureBox for drawing, you
should read the GDI+ FAQ. Specifically the article regarding PictureBox

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

As for saving the Bitmap you could inside your Paint procedure separate
actual drawing to a method that you pass either the Paint graphics object,
or a Bitmap graphics object, otherwise refer to the FAQ

--
Happy Coding!
Morten Wennevik [C# MVP]
Oct 24 '06 #2
Lee
I've done the following

In OnPaint added this

Bitmap bmp = new Bitmap(this.Width, this.Height);
Graphics g = Graphics.FromImage(bmp);

Then for my painting method, simply duplicated it so that it paints to
g as well.
Now I get a nice black bitmap and not a lot else :(

Thanks for the link to the FAQ but I'm not sure how I can refactor my
current code to make use of it, due to deadlines I think it might have
to wait for the next project.

Cheers

Morten Wennevik wrote:
Hi Lee

Before Bob Powell notices you are using a PictureBox for drawing, you
should read the GDI+ FAQ. Specifically the article regarding PictureBox

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

As for saving the Bitmap you could inside your Paint procedure separate
actual drawing to a method that you pass either the Paint graphics object,
or a Bitmap graphics object, otherwise refer to the FAQ

--
Happy Coding!
Morten Wennevik [C# MVP]
Oct 24 '06 #3
Graphics g = Graphics.FromImage(bmp);

The above line is good.
Therefore do you then do all of the drawing with THAT graphics object?
Then after the drawing has finished save the bitmap (bmp) to disk?

That (from what I recall) should do the trick.

Simon Tamman

"Lee" <lv******@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
I've done the following

In OnPaint added this

Bitmap bmp = new Bitmap(this.Width, this.Height);
Graphics g = Graphics.FromImage(bmp);

Then for my painting method, simply duplicated it so that it paints to
g as well.
Now I get a nice black bitmap and not a lot else :(

Thanks for the link to the FAQ but I'm not sure how I can refactor my
current code to make use of it, due to deadlines I think it might have
to wait for the next project.

Cheers

Morten Wennevik wrote:
Hi Lee

Before Bob Powell notices you are using a PictureBox for drawing, you
should read the GDI+ FAQ. Specifically the article regarding PictureBox

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

As for saving the Bitmap you could inside your Paint procedure separate
actual drawing to a method that you pass either the Paint graphics
object,
or a Bitmap graphics object, otherwise refer to the FAQ

--
Happy Coding!
Morten Wennevik [C# MVP]

Oct 24 '06 #4
Lee
Yep that is exactly what I'm doing, but all I get back is a black
bitmap.

I've tried also: Graphics g = Graphics.FromImage(this.Image);

which yeilds the same results

Simon Tamman wrote:
Graphics g = Graphics.FromImage(bmp);

The above line is good.
Therefore do you then do all of the drawing with THAT graphics object?
Then after the drawing has finished save the bitmap (bmp) to disk?

That (from what I recall) should do the trick.

Simon Tamman

"Lee" <lv******@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
I've done the following

In OnPaint added this

Bitmap bmp = new Bitmap(this.Width, this.Height);
Graphics g = Graphics.FromImage(bmp);

Then for my painting method, simply duplicated it so that it paints to
g as well.
Now I get a nice black bitmap and not a lot else :(

Thanks for the link to the FAQ but I'm not sure how I can refactor my
current code to make use of it, due to deadlines I think it might have
to wait for the next project.

Cheers

Morten Wennevik wrote:
Hi Lee
>
Before Bob Powell notices you are using a PictureBox for drawing, you
should read the GDI+ FAQ. Specifically the article regarding PictureBox
>
http://www.bobpowell.net/pictureboxhowto.htm
>
As for saving the Bitmap you could inside your Paint procedure separate
actual drawing to a method that you pass either the Paint graphics
object,
or a Bitmap graphics object, otherwise refer to the FAQ
>
--
Happy Coding!
Morten Wennevik [C# MVP]
Oct 24 '06 #5
Lee
Got it working, here is the entire code
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Collections;
namespace FINN
{

public class flickerFreePictureBox : PictureBox
{
public ArrayList Lines = new ArrayList();
public Bitmap bmp;
private Graphics g;
public flickerFreePictureBox()
{
bmp = new Bitmap(800, 310);
g = Graphics.FromImage(bmp);
g.FillRectangle(System.Drawing.Brushes.White, 0,0, 800, 310);
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
//this.MouseMove += new MouseEventHandler(_MouseMove);

}
protected override void OnPaint(PaintEventArgs e)
{
bool error = false;

//e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
try
{
foreach(scribbleObject al in Lines)
{
bool Drawdot = false;
try
{
Point[] linelist = new Point[al.linePositions.Count];
al.linePositions.CopyTo(0,linelist,0,al.linePositi ons.Count);

e.Graphics.DrawLines(al.pen(), linelist);
g.DrawLines(al.pen(), linelist);

}
catch
{
Drawdot = true; //we want to draw a dot
}
if(Drawdot)
{
//Draw a single dot as the user has only clicked, rather than
clicked and dragged
//TODO: Draw a dot
}
}
}
catch
{
error = true;
}
if(error)
{
//this.OnPaint(e);
}
bmp.Save("Test.bmp");

}
}

}
I know there are magic numbers in there, but for some reason
'this.Width' etc wasn't working when I tried it on the setup, got too
many other things to do to refine it, but if I get some time I'll make
it neater, cheers for your help all
Lee wrote:
Yep that is exactly what I'm doing, but all I get back is a black
bitmap.

I've tried also: Graphics g = Graphics.FromImage(this.Image);

which yeilds the same results

Simon Tamman wrote:
Graphics g = Graphics.FromImage(bmp);

The above line is good.
Therefore do you then do all of the drawing with THAT graphics object?
Then after the drawing has finished save the bitmap (bmp) to disk?

That (from what I recall) should do the trick.

Simon Tamman

"Lee" <lv******@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
I've done the following
>
In OnPaint added this
>
Bitmap bmp = new Bitmap(this.Width, this.Height);
Graphics g = Graphics.FromImage(bmp);
>
Then for my painting method, simply duplicated it so that it paints to
g as well.
Now I get a nice black bitmap and not a lot else :(
>
Thanks for the link to the FAQ but I'm not sure how I can refactor my
current code to make use of it, due to deadlines I think it might have
to wait for the next project.
>
Cheers
>
Morten Wennevik wrote:
Hi Lee

Before Bob Powell notices you are using a PictureBox for drawing, you
should read the GDI+ FAQ. Specifically the article regarding PictureBox

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

As for saving the Bitmap you could inside your Paint procedure separate
actual drawing to a method that you pass either the Paint graphics
object,
or a Bitmap graphics object, otherwise refer to the FAQ

--
Happy Coding!
Morten Wennevik [C# MVP]
>
Oct 24 '06 #6
Eh?

Open a new windows project, drop a picture box onto the form and paste the
following code in.
That should work.
This code not only saves the file to disk (c:\test.bmp) but also reloads it
from disk to the picturebox and works fine for me.

Then track back from this code onto your code and see where you're going
wrong.

private string path = "c:\\test.bmp";

public Form1()
{
InitializeComponent();
this.Load += new EventHandler(Form1_Load);
}

void Form1_Load(object sender, EventArgs e)
{
using (Bitmap b = new Bitmap(100, 100))
{
using (Graphics g = Graphics.FromImage(b))
{
using (SolidBrush sb = new SolidBrush(Color.Red))
{
g.FillRectangle(sb, 0, 0, 50, 50);
}
using (SolidBrush sb = new SolidBrush(Color.Green))
{
g.FillRectangle(sb, 50, 50, 50, 50);
}
b.Save(path, System.Drawing.Imaging.ImageFormat.Bmp);
}
}
LoadBitmap();
}

private void LoadBitmap()
{
Bitmap b = (Bitmap)Bitmap.FromFile(path);
this.pictureBox1.Image = b;
}

"Lee" <lv******@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
Yep that is exactly what I'm doing, but all I get back is a black
bitmap.

I've tried also: Graphics g = Graphics.FromImage(this.Image);

which yeilds the same results

Simon Tamman wrote:
Graphics g = Graphics.FromImage(bmp);

The above line is good.
Therefore do you then do all of the drawing with THAT graphics object?
Then after the drawing has finished save the bitmap (bmp) to disk?

That (from what I recall) should do the trick.

Simon Tamman

"Lee" <lv******@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
I've done the following
>
In OnPaint added this
>
Bitmap bmp = new Bitmap(this.Width, this.Height);
Graphics g = Graphics.FromImage(bmp);
>
Then for my painting method, simply duplicated it so that it paints to
g as well.
Now I get a nice black bitmap and not a lot else :(
>
Thanks for the link to the FAQ but I'm not sure how I can refactor my
current code to make use of it, due to deadlines I think it might have
to wait for the next project.
>
Cheers
>
Morten Wennevik wrote:
Hi Lee

Before Bob Powell notices you are using a PictureBox for drawing,
you
should read the GDI+ FAQ. Specifically the article regarding
PictureBox

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

As for saving the Bitmap you could inside your Paint procedure
separate
actual drawing to a method that you pass either the Paint graphics
object,
or a Bitmap graphics object, otherwise refer to the FAQ

--
Happy Coding!
Morten Wennevik [C# MVP]
>

Oct 24 '06 #7
Glad to hear you got it working.
I'd re-iterate Morten's advice, Bob Powells GDI+ FAQ is a great FAQ to read
on this kind of thing.

Additionally i'd watch out for the way you're performing the painting the
segment, performing a try catch to determine the user's action is a BAD way
to work things out you'd be better off working out what the user did without
using exceptions.
Secondly I know you currently have it commented out but calling OnPaint
within OnPaint is scary recursion which will lead you into the badness of
StackOverflowExceptions.

I'd recommend reading:
http://www.amazon.co.uk/Framework-De.../dp/0321246756
for a better understanding of exception handling (it has an excellent
chapter on it), it's also a pretty good book for plenty of other reasons.

HTH

Simon
"Lee" <lv******@gmail.comwrote in message
news:11*********************@e3g2000cwe.googlegrou ps.com...
Got it working, here is the entire code
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Collections;
namespace FINN
{

public class flickerFreePictureBox : PictureBox
{
public ArrayList Lines = new ArrayList();
public Bitmap bmp;
private Graphics g;
public flickerFreePictureBox()
{
bmp = new Bitmap(800, 310);
g = Graphics.FromImage(bmp);
g.FillRectangle(System.Drawing.Brushes.White, 0,0, 800, 310);
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
//this.MouseMove += new MouseEventHandler(_MouseMove);

}
protected override void OnPaint(PaintEventArgs e)
{
bool error = false;

//e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
try
{
foreach(scribbleObject al in Lines)
{
bool Drawdot = false;
try
{
Point[] linelist = new Point[al.linePositions.Count];
al.linePositions.CopyTo(0,linelist,0,al.linePositi ons.Count);

e.Graphics.DrawLines(al.pen(), linelist);
g.DrawLines(al.pen(), linelist);

}
catch
{
Drawdot = true; //we want to draw a dot
}
if(Drawdot)
{
//Draw a single dot as the user has only clicked, rather than
clicked and dragged
//TODO: Draw a dot
}
}
}
catch
{
error = true;
}
if(error)
{
//this.OnPaint(e);
}
bmp.Save("Test.bmp");

}
}

}
I know there are magic numbers in there, but for some reason
'this.Width' etc wasn't working when I tried it on the setup, got too
many other things to do to refine it, but if I get some time I'll make
it neater, cheers for your help all
Lee wrote:
Yep that is exactly what I'm doing, but all I get back is a black
bitmap.

I've tried also: Graphics g = Graphics.FromImage(this.Image);

which yeilds the same results

Simon Tamman wrote:
Graphics g = Graphics.FromImage(bmp);
>
The above line is good.
Therefore do you then do all of the drawing with THAT graphics object?
Then after the drawing has finished save the bitmap (bmp) to disk?
>
That (from what I recall) should do the trick.
>
Simon Tamman
>
"Lee" <lv******@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
I've done the following

In OnPaint added this

Bitmap bmp = new Bitmap(this.Width, this.Height);
Graphics g = Graphics.FromImage(bmp);

Then for my painting method, simply duplicated it so that it paints
to
g as well.
Now I get a nice black bitmap and not a lot else :(

Thanks for the link to the FAQ but I'm not sure how I can refactor
my
current code to make use of it, due to deadlines I think it might
have
to wait for the next project.

Cheers

Morten Wennevik wrote:
Hi Lee
>
Before Bob Powell notices you are using a PictureBox for drawing,
you
should read the GDI+ FAQ. Specifically the article regarding
PictureBox
>
http://www.bobpowell.net/pictureboxhowto.htm
>
As for saving the Bitmap you could inside your Paint procedure
separate
actual drawing to a method that you pass either the Paint graphics
object,
or a Bitmap graphics object, otherwise refer to the FAQ
>
--
Happy Coding!
Morten Wennevik [C# MVP]

Oct 24 '06 #8
Lee
OnPaint is commented out for exactly that reason! :-)

I agree with you on the try/catch.

I'm fully aware that the code is a horrible hack, it was written months
ago and was my first real attempt and dealing with System.Drawing et
al. Unfortunately deadlines loom and I don't have time to refactor
it...

The moral of the story: get it right the first time.

Simon Tamman wrote:
Glad to hear you got it working.
I'd re-iterate Morten's advice, Bob Powells GDI+ FAQ is a great FAQ to read
on this kind of thing.

Additionally i'd watch out for the way you're performing the painting the
segment, performing a try catch to determine the user's action is a BAD way
to work things out you'd be better off working out what the user did without
using exceptions.
Secondly I know you currently have it commented out but calling OnPaint
within OnPaint is scary recursion which will lead you into the badness of
StackOverflowExceptions.

I'd recommend reading:
http://www.amazon.co.uk/Framework-De.../dp/0321246756
for a better understanding of exception handling (it has an excellent
chapter on it), it's also a pretty good book for plenty of other reasons.

HTH

Simon
"Lee" <lv******@gmail.comwrote in message
news:11*********************@e3g2000cwe.googlegrou ps.com...
Got it working, here is the entire code
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Collections;
namespace FINN
{

public class flickerFreePictureBox : PictureBox
{
public ArrayList Lines = new ArrayList();
public Bitmap bmp;
private Graphics g;
public flickerFreePictureBox()
{
bmp = new Bitmap(800, 310);
g = Graphics.FromImage(bmp);
g.FillRectangle(System.Drawing.Brushes.White, 0,0, 800, 310);
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
//this.MouseMove += new MouseEventHandler(_MouseMove);

}
protected override void OnPaint(PaintEventArgs e)
{
bool error = false;

//e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
try
{
foreach(scribbleObject al in Lines)
{
bool Drawdot = false;
try
{
Point[] linelist = new Point[al.linePositions.Count];
al.linePositions.CopyTo(0,linelist,0,al.linePositi ons.Count);

e.Graphics.DrawLines(al.pen(), linelist);
g.DrawLines(al.pen(), linelist);

}
catch
{
Drawdot = true; //we want to draw a dot
}
if(Drawdot)
{
//Draw a single dot as the user has only clicked, rather than
clicked and dragged
//TODO: Draw a dot
}
}
}
catch
{
error = true;
}
if(error)
{
//this.OnPaint(e);
}
bmp.Save("Test.bmp");

}
}

}
I know there are magic numbers in there, but for some reason
'this.Width' etc wasn't working when I tried it on the setup, got too
many other things to do to refine it, but if I get some time I'll make
it neater, cheers for your help all
Lee wrote:
Yep that is exactly what I'm doing, but all I get back is a black
bitmap.
>
I've tried also: Graphics g = Graphics.FromImage(this.Image);
>
which yeilds the same results
>
Simon Tamman wrote:
Graphics g = Graphics.FromImage(bmp);

The above line is good.
Therefore do you then do all of the drawing with THAT graphics object?
Then after the drawing has finished save the bitmap (bmp) to disk?

That (from what I recall) should do the trick.

Simon Tamman

"Lee" <lv******@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
I've done the following
>
In OnPaint added this
>
Bitmap bmp = new Bitmap(this.Width, this.Height);
Graphics g = Graphics.FromImage(bmp);
>
Then for my painting method, simply duplicated it so that it paints
to
g as well.
Now I get a nice black bitmap and not a lot else :(
>
Thanks for the link to the FAQ but I'm not sure how I can refactor
my
current code to make use of it, due to deadlines I think it might
have
to wait for the next project.
>
Cheers
>
Morten Wennevik wrote:
Hi Lee

Before Bob Powell notices you are using a PictureBox for drawing,
you
should read the GDI+ FAQ. Specifically the article regarding
PictureBox

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

As for saving the Bitmap you could inside your Paint procedure
separate
actual drawing to a method that you pass either the Paint graphics
object,
or a Bitmap graphics object, otherwise refer to the FAQ

--
Happy Coding!
Morten Wennevik [C# MVP]
>
Oct 24 '06 #9
On 2006-10-24, Lee <lv******@gmail.comwrote:
public class flickerFreePictureBox : PictureBox

Assuming you have an instance named myFlickerFreePictureBox:

int width = myFlickerFreePictureBox.Width;
int height = myFlickerFreePictureBox.Height;

Bitmap bitmap = new Bitmap(width, height);
myFlickerFreePictureBox.DrawToBitmap(bitmap, new Rectangle(0, 0, width, height));

bitmap.Save(@"C:\somewhere.gif", ImageFormat.Gif);
--
Met vriendelijke groeten,
Tim Van Wassenhove <http://www.timvw.be>
Oct 24 '06 #10
;-)

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"Morten Wennevik" <Mo************@hotmail.comwrote in message
news:op***************@tr024.bouvet.no...
Hi Lee

Before Bob Powell notices you are using a PictureBox for drawing, you
should read the GDI+ FAQ. Specifically the article regarding PictureBox

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

As for saving the Bitmap you could inside your Paint procedure separate
actual drawing to a method that you pass either the Paint graphics object,
or a Bitmap graphics object, otherwise refer to the FAQ

--
Happy Coding!
Morten Wennevik [C# MVP]

Oct 24 '06 #11
Despite Morten's fears for your health, Congratulations, you've done it in
more or less the right way by overriding the control and staying on top of
the functionality using "OOP" rather than "POO" which is, of course, the
opposite of OOP.

I assume that somewhere you loaded an image and put it into the Image
property of the flickerFreePictureBox so...

All you need to do is:

#1 refactor your onpaint so that you can call a PaintObjects method handing
in a Graphics object.

#2 In OnPaint, call PaintObjects(e.Graphics) which will iterate your list
and draw your lines and dots.

#3In the Save method do this...

// this code may not compile but you'll see the sense of it...

Bitmap bm=new Bitmap(orixinal.Width, original.Height, PixelFormat.24BppRGB);
Graphics g=Graphics.FromImage(bm);
g.DrawImageUnscaled(original);
PaintObjects(g);
g.Dispose();
bm.Save(<some filename>,<Some format>);

Hope this helps.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"Lee" <lv******@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
>I have the following code
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Collections;
namespace FINN
{

public class flickerFreePictureBox : PictureBox
{
public ArrayList Lines = new ArrayList();
public flickerFreePictureBox()
{

this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle (ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
//this.MouseMove += new MouseEventHandler(_MouseMove);
}
protected override void OnPaint(PaintEventArgs e)
{
bool error = false;
//e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
try
{
foreach(scribbleObject al in Lines)
{
bool Drawdot = false;
try
{
Point[] linelist = new Point[
al.linePositions.Count];

al.linePositions.CopyTo(0,linelist,0,al.linePositi ons.Count);
e.Graphics.DrawLines(al.pen(), linelist);
}
catch
{
Drawdot = true; //we want to draw a dot
}
if(Drawdot)
{
//Draw a single dot as the user has only
clicked, rather than clicked and dragged
//TODO: Draw a dot
}

}
}
catch
{
error = true;
}
if(error)
{
//this.OnPaint(e);
}
}
}

}

And I would like to be able to create a bitmap of what gets drawn in
OnPaint so that I can save it to file, I've tried a few things, but so
far just ended up with black bitmaps the same size as the control that
code creates, any suggestions or code snippets would be a great help.

Cheers

Lee

Oct 24 '06 #12
Hi Simon,

As an alternative to copy the code in OnPaint for your Bitmap drawing, you
can create a PaintEventArgs and call OnPaint directly.

void SaveBitmap()
{
using (Graphics g = Graphics.FromImage(bmp))
{
PaintEventArgs pea = new PaintEventArgs(g, new
Rectangle(0, 0, b.Width, b.Height));
OnPaint(pea);
}
bmp.Save(...);
}

--
Happy Coding!
Morten Wennevik [C# MVP]
Oct 25 '06 #13

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

Similar topics

6
by: charsh | last post by:
Hi, I using the code below to draw a text in a pictureBox1. //Start--------------------------------------------------------------- private void button1_Click(object sender, System.EventArgs e)...
9
by: Mark Johnson | last post by:
How can you save all or a portion of the Grafics object to a Image/Bitmap ? I am try to save the Images from Cards.dll to a BitMap file. I can read in the Images to the Grafics, but when I try this...
12
by: yaya via DotNetMonster.com | last post by:
Hi, I have a picture box with circles and rectangles, and I wana save all the images into a jpg file, i tried pictureBox1.Image.Save(@"c:\1.jpg"); but I got and error...
2
by: Olaf Baeyens | last post by:
I want to convert one bitmap file to another one. For example load as bmp and save as jpg. The loading part is simple I do this: Stream BitmapStream =...
2
by: Peter Proost | last post by:
Hi group when save a bitmap called saveBmp like this: <<<< saveBmp.Save(filename, ImageFormat.Jpeg) <<<< the bitmap gets saved with it's size propertys, so if I right click the file and...
1
by: WB | last post by:
Hi, I have a helper class that has a method to resize images. It takes the virtual path of an image and resize it to a specified dimension like this. public void ResizeImageTest(string...
1
by: Stedak | last post by:
I have the following class I use to save Tiff's. The problem I have with it is that the final size of the images are very large. If we scan directly to a file the final tiff may be 600-900 kb.s but...
13
by: Joe | last post by:
does anyone know why this source code throws an error?? Dim myBitmap As System.Drawing.Bitmap Dim myGraphics As Graphics myBitmap = New System.Drawing.Bitmap(fileName:="C:\test.tif")...
2
by: Piotrekk | last post by:
Hi I have a problem. I open bitmap file, load it to memory decrease color palette by proceeding Euclidean distance. As a result i have image reduced to - for example 18 colors. Once i save it -...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.