From: kndg on
Alberto wrote:
> 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.
From: Alberto on
but I can't do this because e is an eventArgs.

"kndg" <reply(a)this.newsgroup> escribi� en el mensaje
news:OQFmv6xRKHA.4692(a)TK2MSFTNGP06.phx.gbl...
> Alberto wrote:
>> 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.


From: kndg on
Alberto wrote:
> 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.

> "kndg" <reply(a)this.newsgroup> escribi� en el mensaje
> news:OQFmv6xRKHA.4692(a)TK2MSFTNGP06.phx.gbl...
>> Alberto wrote:
>>> 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.
>
>
From: Alberto on
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


"kndg" <reply(a)this.newsgroup> escribi� en el mensaje
news:uLjjgnyRKHA.4048(a)TK2MSFTNGP05.phx.gbl...
> Alberto wrote:
>> 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.
>
>> "kndg" <reply(a)this.newsgroup> escribi� en el mensaje
>> news:OQFmv6xRKHA.4692(a)TK2MSFTNGP06.phx.gbl...
>>> Alberto wrote:
>>>> 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.
>>

From: kndg on
Alberto wrote:
> 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
>
> [...]