Click here to Skip to main content
15,894,825 members
Home / Discussions / C#
   

C#

 
GeneralWav To Midi Pin
MicSky28-Jul-04 21:17
MicSky28-Jul-04 21:17 
GeneralRe: Wav To Midi Pin
Bret Mulvey9-Aug-04 18:13
Bret Mulvey9-Aug-04 18:13 
GeneralSimple yet unanswered question Pin
hatim_ali28-Jul-04 19:16
hatim_ali28-Jul-04 19:16 
GeneralRe: Simple yet unanswered question Pin
J. Dunlap28-Jul-04 19:30
J. Dunlap28-Jul-04 19:30 
GeneralRe: Simple yet unanswered question Pin
Giles28-Jul-04 20:57
Giles28-Jul-04 20:57 
Questionsave bitmap ? Pin
kendao28-Jul-04 17:33
kendao28-Jul-04 17:33 
AnswerRe: save bitmap ? Pin
mav.northwind28-Jul-04 21:13
mav.northwind28-Jul-04 21:13 
AnswerRe: save bitmap ? Pin
Heath Stewart29-Jul-04 5:14
protectorHeath Stewart29-Jul-04 5:14 
What 'mav' said will work, but there's several problems with his approach. If you draw on a Graphics object that you obtained from Control.CreateGraphics, your elements will be drawn. But if the form needs repainting (perhaps it was covered up by another form), they won't be redrawn. You need to perform your drawing in an override of OnPaint (which is passed a PaintEventArgs that has a Graphics property that you should use for painting).

In order to be able to save that same code to a Bitmap class and then to a file, you'll want to modularize your code like so:
private void PaintRectangles(Graphics g)
{
  g.DrawRectangle(...); // Draw rectangles or whatever here
}
protected override void OnPaint(PaintEventArgs e)
{
  base.OnPaint(e);
  PaintRectangles(e.Graphics);
}
private void saveBtn_Click(object sender, EventArgs e)
{
  using (Bitmap bmp = new Bitmap(panel1.Size.Width,
    panel1.Size.Height))
  {
    using (Graphics g = Graphics.FromImage(bmp))
    {
      PaintRectangles(g);
    }
    bmp.Save("filename.png", ImageFormat.Png); // Save as PNG file
  }
}
Now you've encapsulated your rectangle drawing code into a single method that can draw onto any Graphics object, including the form or panel itself (this is only an example, mind you) or a Bitmap. The using statements above make sure that both the Bitmap and Graphics objects are disposed (very important, otherwise memory may not be freed when necessary and unmanaged objects will linger, causing memory leaks) even if an exception is thrown.

 

Microsoft MVP, Visual C#
My Articles
Generaltabbing with text selection Pin
azusakt28-Jul-04 16:45
azusakt28-Jul-04 16:45 
GeneralRe: tabbing with text selection Pin
mikker_12328-Jul-04 23:48
mikker_12328-Jul-04 23:48 
GeneralHTTP POST with unicode in C# Pin
khchan28-Jul-04 16:32
khchan28-Jul-04 16:32 
GeneralRe: HTTP POST with unicode in C# Pin
Heath Stewart29-Jul-04 5:17
protectorHeath Stewart29-Jul-04 5:17 
QuestionFast way to link items in richTextBoxes with text ? Pin
evdoxos28-Jul-04 14:41
evdoxos28-Jul-04 14:41 
AnswerRe: Fast way to link items in richTextBoxes with text ? Pin
mav.northwind28-Jul-04 21:21
mav.northwind28-Jul-04 21:21 
GeneralRe: Fast way to link items in richTextBoxes with text ? Pin
evdoxos29-Jul-04 0:59
evdoxos29-Jul-04 0:59 
GeneralRe: Fast way to link items in richTextBoxes with text ? Pin
mav.northwind29-Jul-04 1:04
mav.northwind29-Jul-04 1:04 
GeneralRe: Fast way to link items in richTextBoxes with text ? Pin
evdoxos29-Jul-04 2:13
evdoxos29-Jul-04 2:13 
GeneralRe: Fast way to link items in richTextBoxes with text ? Pin
mav.northwind29-Jul-04 2:30
mav.northwind29-Jul-04 2:30 
GeneralRe: Fast way to link items in richTextBoxes with text ? Pin
evdoxos29-Jul-04 18:26
evdoxos29-Jul-04 18:26 
GeneralStarting a GUI app with a windows service in C# Pin
satorical28-Jul-04 14:27
satorical28-Jul-04 14:27 
GeneralRe: Starting a GUI app with a windows service in C# Pin
ChrisAdams28-Jul-04 15:25
ChrisAdams28-Jul-04 15:25 
GeneralRe: Starting a GUI app with a windows service in C# Pin
Heath Stewart29-Jul-04 5:23
protectorHeath Stewart29-Jul-04 5:23 
GeneralRe: Starting a GUI app with a windows service in C# Pin
satorical29-Jul-04 6:13
satorical29-Jul-04 6:13 
GeneralRe: Starting a GUI app with a windows service in C# Pin
Heath Stewart29-Jul-04 6:23
protectorHeath Stewart29-Jul-04 6:23 
Questionhow to get cd information Pin
civc28-Jul-04 13:23
civc28-Jul-04 13:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.