Click here to Skip to main content
15,903,854 members
Home / Discussions / C#
   

C#

 
GeneralRe: Launching an event with DateTime Pin
Dave Kreskowiak1-Nov-04 10:26
mveDave Kreskowiak1-Nov-04 10:26 
GeneralRe: Launching an event with DateTime Pin
Heath Stewart1-Nov-04 14:11
protectorHeath Stewart1-Nov-04 14:11 
GeneralRe: Launching an event with DateTime Pin
Dave Kreskowiak2-Nov-04 0:50
mveDave Kreskowiak2-Nov-04 0:50 
GeneralRe: Launching an event with DateTime Pin
goldoche2-Nov-04 1:47
goldoche2-Nov-04 1:47 
GeneralRe: Launching an event with DateTime Pin
goldoche2-Nov-04 1:37
goldoche2-Nov-04 1:37 
GeneralRe: WindowsPrincipal and Active Directory Pin
Heath Stewart1-Nov-04 8:37
protectorHeath Stewart1-Nov-04 8:37 
GeneralEnter or Arrow Key to replace Tab Key Pin
FredSP1-Nov-04 7:22
FredSP1-Nov-04 7:22 
GeneralRe: Enter or Arrow Key to replace Tab Key Pin
Alex Korchemniy1-Nov-04 8:53
Alex Korchemniy1-Nov-04 8:53 
GeneralRe: Enter or Arrow Key to replace Tab Key Pin
Heath Stewart1-Nov-04 9:26
protectorHeath Stewart1-Nov-04 9:26 
GeneralRe: Enter or Arrow Key to replace Tab Key Pin
Alex Korchemniy1-Nov-04 9:31
Alex Korchemniy1-Nov-04 9:31 
Generalabout memory management Pin
momer1-Nov-04 7:04
momer1-Nov-04 7:04 
GeneralRe: about memory management Pin
Dave Kreskowiak1-Nov-04 7:42
mveDave Kreskowiak1-Nov-04 7:42 
GeneralRe: about memory management Pin
momer1-Nov-04 8:16
momer1-Nov-04 8:16 
GeneralShellExecute in C# Pin
ben21-Nov-04 7:00
ben21-Nov-04 7:00 
GeneralRe: ShellExecute in C# Pin
ben21-Nov-04 7:04
ben21-Nov-04 7:04 
Questiondo i need to double buffer? Pin
vista271-Nov-04 6:26
vista271-Nov-04 6:26 
AnswerRe: do i need to double buffer? Pin
Dave Kreskowiak1-Nov-04 6:41
mveDave Kreskowiak1-Nov-04 6:41 
GeneralTreeView Scrollbar Pin
amadobson1-Nov-04 6:12
amadobson1-Nov-04 6:12 
Questionsave GDI drawing and print them? Pin
xiaowenjie1-Nov-04 6:11
xiaowenjie1-Nov-04 6:11 
AnswerRe: save GDI drawing and print them? Pin
Heath Stewart1-Nov-04 8:40
protectorHeath Stewart1-Nov-04 8:40 
GeneralRe: save GDI drawing and print them? Pin
xiaowenjie1-Nov-04 18:25
xiaowenjie1-Nov-04 18:25 
GeneralRe: save GDI drawing and print them? Pin
Heath Stewart2-Nov-04 5:04
protectorHeath Stewart2-Nov-04 5:04 
The first error that I noticed right away: never store your Graphics object. You either use the one passed to you in the PaintEventArgs from your OnPaint override, or create one for your control using Control.CreateGraphics (if you want a compatible bitmap on which to draw; having a compatible bitmap means the measurement units are the same, the scaling factors, etc., as the control itself). If you create a Graphics object for a Bitmap, at least use a bitmap that is the same size as the control.

In addition to not storing the Graphics, you shouldn't even store the Bitmap (factors can change; if the user changes the screen resolution or DPI settings, for example, your bitmap may not look right because GDI+ will now draw differently), and always dispose of objects like Graphics and Bitmap to make sure their native, unmanaged resources are freed immediately (they're unmanaged because the GC doesn't know about them and won't free them automatically like it does for managed objects). If not, that memory may remain allocated for some time until the GC gets around to finalizing (destroying) the object; and the only reason the native resources are freed is because the classes follow the disposable pattern (the finalizer disposes only unmanaged objects, while the IDisposable implementation disposes both unmanaged and managed objects immediately).

The code you should be using would be similar to the following:
class Graph : Form
{
  protected override void OnPaint(PaintEventArgs e)
  {
    base.OnPaint(e);
    DrawImage(e.Graphics);
  }
 
  private void DrawImage(Graphics g)
  {
    // ...
  }
 
  // The caller should be sure to call Dispose() on the bitmap
  // when finished with it to free memory immediately.
  public Bitmap ReturnImage() // "GetImage" would be a more consistent name
  {
    Bitmap bmp = new Bitmap(Width, Height);
    using (Graphics g = Graphics.FromImage(bmp))
      DrawImage(g);
 
    return bmp;
  }
 
  void SaveDrawing()
  {
    using (SaveFileDialog dialog = new SaveFileDialog())
    {
      // Initialize the dialog...
 
      if (dialog.ShowDialog() == DialogResult.OK)
        using (Bitmap bmp = ReturnImage())
          bmp.Save(dialog.FileName, ImageFormat.Bmp);
    }
  }
 
  // ...
}


This posting is provided "AS IS" with no warranties, and confers no rights.

Software Design Engineer
Developer Division Sustained Engineering
Microsoft

[My Articles] [My Blog]
Questionreader bar using GDI+? Pin
xiaowenjie1-Nov-04 5:46
xiaowenjie1-Nov-04 5:46 
AnswerRe: reader bar using GDI+? Pin
Alex Korchemniy1-Nov-04 8:40
Alex Korchemniy1-Nov-04 8:40 
Questionmy drawing Moves?? Pin
xiaowenjie1-Nov-04 5:40
xiaowenjie1-Nov-04 5:40 

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.