Save to jpg

A

Alberto

I have a Panel with some Labels and lines who were draw using Graphics and
the method DrawLine().
When I use the method DrawToBitmap() the image saved to disk has the labels
but doesn't have the lines.

Does anybody know why?
Is it posible to save the image to a jpg file instead to bmp?

Thak you very much.
 
P

Peter Duniho

I have a Panel with some Labels and lines who were draw using Graphics
and the method DrawLine().
When I use the method DrawToBitmap() the image saved to disk has the
labels but doesn't have the lines.

Does anybody know why?

No. You didn't post any code showing what you're actually doing.
Obviously, the lines aren't being drawn as part of the operation invoked
by the call to DrawToBitmap(), but without specific information, there's
nothing to be said about why that might be.
Is it posible to save the image to a jpg file instead to bmp?

Yes. There are Save() method overloads that let you do that.

Pete
 
A

Alberto

This is the code who save the content of the Panel in a bmp file.

SaveFileDialog saveFileDialog = new SaveFileDialog();

saveFileDialog.DefaultExt = "bmp";
saveFileDialog.Filter = "Bitmap files|*.bmp";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
int width = panel.Width;
int height = panel.Height;

Bitmap bitMap = new Bitmap(width,height);
Rectangle rec = new Rectangle(0, 0,width,height);

panel.DrawToBitmap(bitMap, rec);

bitMap.Save(saveFileDialog.FileName);
}

Could you tell me how to save in jpg instead than a bmp?
Thank you
 
A

Alberto

Thank you.

Do you know why the lines that I draw with DrawLine aren't save in the file?
 
A

Alberto

I just wrote in another proyect this code:

private void button5_Click(object sender, EventArgs e)

{

Graphics g = panel1.CreateGraphics();

g.DrawLine(new Pen(Brushes.Blue), new Point(0, 0), new Point(50, 50));

}

private void button4_Click(object sender, EventArgs e)

{

int width = panel1.Width;

int height = panel1.Height;

Bitmap bitMap = new Bitmap(width, height);

Rectangle rec = new Rectangle(0, 0, width, height);

panel1.DrawToBitmap(bitMap, rec);

bitMap.Save(@"C:/Windows/prueba.bmp");

}

The problem is the same. The line didn't appear in the file stored.

Thank you for your help

Mark Rae said:
[please don't top-post]
Do you know why the lines that I draw with DrawLine aren't saved in the
file?

Not without seeing your code...
 
J

Jeff Gaines

The problem is the same. The line didn't appear in the file stored.

Does the order of clicking the buttons (4 & 5) affect what is saved? It
may be that the Button 4 click function over-writes what was drawn in the
Button 5 click function.
 
A

Alberto

The code of the button4 doesn't delete the graphic area of the panel. If I
draw the line in the panel Paint event, the results are the same.
 
K

kndg

Alberto said:
I just wrote in another proyect this code:

private void button5_Click(object sender, EventArgs e)

{

Graphics g = panel1.CreateGraphics();

g.DrawLine(new Pen(Brushes.Blue), new Point(0, 0), new Point(50, 50));

}

private void button4_Click(object sender, EventArgs e)

{

int width = panel1.Width;

int height = panel1.Height;

Bitmap bitMap = new Bitmap(width, height);

Rectangle rec = new Rectangle(0, 0, width, height);

panel1.DrawToBitmap(bitMap, rec);

bitMap.Save(@"C:/Windows/prueba.bmp");

}

The problem is the same. The line didn't appear in the file stored.

Thank you for your help

Mark Rae said:
[please don't top-post]
Could you tell me how to save in jpg instead than a bmp?
Bitmap bitMap = new Bitmap(panel.Width, panel.Height);
panel.DrawToBitmap(bitMap, new Rectangle(0, 0, panel.Width,
panel.Height));
bitMap.Save(saveFileDialog.FileName,
System.Drawing.Imaging.ImageFormat.Jpeg);
Do you know why the lines that I draw with DrawLine aren't saved in the
file?
Not without seeing your code...

Hi Alberto,

You should put your drawing operation on panel1 on Paint event handler.

Example:
1. double-click panel1 control on the designer
2. add below code on panel1_Paint method

private void panel1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;

g.DrawLine(new Pen(Brushes.Blue), new Point(0, 0), new Point(50, 50));
}

Regards.
 
K

kndg

Alberto said:
In the original project I draw the lines in the paint event and it didn't
work. Anyway I wrote a new project only with the code to draw the line and
save it in a file and didn't work. The project is attached if you want to
see it.

Thank you.

Change

Graphics g = panel1.CreateGraphics();

to

Graphics g = e.Graphics;

and it should solve the problem.

Regards.
 
A

Alberto

but I can't do this because e is an eventArgs.

kndg said:
Change

Graphics g = panel1.CreateGraphics();

to

Graphics g = e.Graphics;

and it should solve the problem.

Regards.
 
K

kndg

Alberto said:
but I can't do this because e is an eventArgs.

I'm sorry, I don't understand the above statement.
Do you mean the button Click event?
Are you trying to do some graphics operation base on button click and
then would like save the generated image?
Maybe below code could give you some idea...

List<Action<Graphics>> drawings = new List<Action<Graphics>>();

private void DrawBlueLine(Graphics g)
{
g.DrawLine(new Pen(Brushes.Blue), new Point(0, 0), new Point(50, 50));
}

private void DrawRedLine(Graphics g)
{
g.DrawLine(new Pen(Brushes.Red), new Point(50, 50), new Point(100, 0));
}

private void button2_Click(object sender, EventArgs e)
{
Graphics g = panel1.CreateGraphics();
DrawBlueLine(g);
g.Dispose();

drawings.Add(DrawBlueLine);
}

private void button3_Click(object sender, EventArgs e)
{
Graphics g = panel1.CreateGraphics();
DrawRedLine(g);
g.Dispose();

drawings.Add(DrawRedLine);
}

private void panel1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;

foreach(var draw in drawings)
{
draw(g);
}
}

Regards.
 
A

Alberto

Sorry but I think I'm explaining myself right (as you can see, English isn't
my language).

In the code you wrote in the previous message, you draw the lines in the
paint event of the Panel but I did it in a previous example and didn't work.

1) I draw the lines in the Paint event of the panel.
2) When the user clicks a button, I create a bitmap and then I fill it with
the content of the panel with this: panel.DrawToBitmap(bitmap,
rectangle);
3) I save the content of the bitmap with bitmap.save(...).

If I draw some pixels in the bitmap, they appear in the saved filed but not
the lines draw firstly so I think that there is some problem with the
DrawToBitmap method.

Thank you for your help
 
K

kndg

Alberto said:
Sorry but I think I'm explaining myself right (as you can see, English isn't
my language).

In the code you wrote in the previous message, you draw the lines in the
paint event of the Panel but I did it in a previous example and didn't work.

It didn't work because you didn't use the graphic object provided by the
Paint event. DrawToBitmap method use this graphic object to 'know' what
are exist on the panel surface and create a bitmap object. If you put a
Button on top of your Panel surface, the image of the button will also
get drawn by this DrawToBitmap method. What you had done is actually
creating another graphic object which is eventually ignored.

I'm actually bad at explaining things, maybe others have some useful
comments/advice for you.

Regards.
1) I draw the lines in the Paint event of the panel.
2) When the user clicks a button, I create a bitmap and then I fill it with
the content of the panel with this: panel.DrawToBitmap(bitmap,
rectangle);
3) I save the content of the bitmap with bitmap.save(...).

If I draw some pixels in the bitmap, they appear in the saved filed but not
the lines draw firstly so I think that there is some problem with the
DrawToBitmap method.

Thank you for your help

[...]
 
P

Peter Duniho

[...]
If I draw some pixels in the bitmap, they appear in the saved filed but
not
the lines draw firstly so I think that there is some problem with the
DrawToBitmap method.

There is not a problem with the DrawToBitmap() method. Many people have
used it successfully to do exactly what you're trying to do (within the
limitations of the method...you should be aware that only those parts of
the control that are visible on the screen will get drawn to the bitmap).

More generally: the assumption that there is something wrong with the OS,
framework library, etc. should be the VERY last resort. Windows, .NET,
etc. are certainly not bug-free. But it's incredibly rare to find
something wrong in any of the basic functionality of the API.

In this particular case, a previous poster indicated that you are calling
CreateGraphics() to get the Graphics instance used to draw your lines. If
that's true, then that indeed is the problem. The only thing that will
appears in the Bitmap after you call DrawToBitmap() are the things that
are drawn during the Paint event, and only those things drawn into the
PaintEventArgs.Graphics instance of a Graphics object.

The previous poster provided alternative code that works, but your reply
suggested that you'd already written code like that and it didn't work.
Since I have no reason to believe the code he posted doesn't work, that
would leave two possibilities: you didn't actually try code like what he
posted, or when you tried it, it did work. Either way, that would mean
you're mistaken about something.

In my very first reply to your question, I hinted that you need to post
the actual code if you expect someone to be able to say specifically what
is wrong with your code. That continues to be true. And posting an
attachment isn't an appropriate way to do that...many ISPs don't even
allow attachments through, either stripping them from the post, or
blocking the post altogether, and attachments aren't archived by the
various Usenet archiving services, including Google Groups. Post a
concise-but-complete code example as text, as part of your message.

I suspect you've been given sufficient information already for you to find
a solution to the problem. But in any case, without a proper code
example, there's not very much point in trying to provide specific
advice. There's too much uncertainty about what's actually wrong with
your code, until you actually post the code.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top