Click here to Skip to main content
15,896,063 members
Home / Discussions / C#
   

C#

 
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 
AnswerRe: my drawing Moves?? Pin
Charlie Williams1-Nov-04 7:11
Charlie Williams1-Nov-04 7:11 
GeneralRe: my drawing Moves?? Pin
xiaowenjie1-Nov-04 7:22
xiaowenjie1-Nov-04 7:22 
GeneralRe: my drawing Moves?? Pin
Charlie Williams1-Nov-04 10:07
Charlie Williams1-Nov-04 10:07 
GeneralRe: my drawing Moves?? Pin
xiaowenjie1-Nov-04 18:46
xiaowenjie1-Nov-04 18:46 
GeneralAccessing .NET Assemblies from COM+ Component Pin
perlmunger1-Nov-04 5:26
perlmunger1-Nov-04 5:26 
GeneralRe: Accessing .NET Assemblies from COM+ Component Pin
Alex Korchemniy1-Nov-04 8:30
Alex Korchemniy1-Nov-04 8:30 
GeneralRe: Accessing .NET Assemblies from COM+ Component Pin
perlmunger1-Nov-04 8:40
perlmunger1-Nov-04 8:40 
GeneralRe: Accessing .NET Assemblies from COM+ Component Pin
Alex Korchemniy1-Nov-04 8:45
Alex Korchemniy1-Nov-04 8:45 
GeneralStreamReader method (object.ReadLine()) Pin
Blubbo1-Nov-04 5:22
Blubbo1-Nov-04 5:22 
GeneralDateTime format Pin
goatstudio1-Nov-04 5:18
goatstudio1-Nov-04 5:18 
GeneralRe: DateTime format Pin
Dave Kreskowiak1-Nov-04 6:32
mveDave Kreskowiak1-Nov-04 6:32 
GeneralRe: DateTime format Pin
goatstudio2-Nov-04 17:15
goatstudio2-Nov-04 17:15 
QuestionHow to do this? Pin
momer1-Nov-04 5:04
momer1-Nov-04 5:04 
AnswerRe: How to do this? Pin
Blubbo1-Nov-04 6:30
Blubbo1-Nov-04 6:30 

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.