Click here to Skip to main content
16,012,468 members
Home / Discussions / Graphics
   

Graphics

 
GeneralRe: JPEG Extended ? Pin
Christian Graus27-Mar-07 2:51
protectorChristian Graus27-Mar-07 2:51 
QuestionDrawing in background thread Pin
Aaron Schaefer9-Mar-07 4:50
Aaron Schaefer9-Mar-07 4:50 
AnswerRe: Drawing in background thread Pin
Mark Salsbery9-Mar-07 6:32
Mark Salsbery9-Mar-07 6:32 
GeneralRe: Drawing in background thread Pin
Aaron Schaefer9-Mar-07 7:27
Aaron Schaefer9-Mar-07 7:27 
QuestionRe: Drawing in background thread Pin
Mark Salsbery9-Mar-07 12:11
Mark Salsbery9-Mar-07 12:11 
AnswerRe: Drawing in background thread Pin
Aaron Schaefer9-Mar-07 15:36
Aaron Schaefer9-Mar-07 15:36 
GeneralRe: Drawing in background thread Pin
Mark Salsbery10-Mar-07 6:38
Mark Salsbery10-Mar-07 6:38 
GeneralRe: Drawing in background thread Pin
vineas22-Mar-07 20:19
vineas22-Mar-07 20:19 
Since your Draw method makes sure that while the background thread is busy, a draw doesn't happen on the bitmap - you don't need to keep creating a new bitmap. In fact, looking at the size of the bitmap you're creating, I bet a lot of your 5-10 second draw is taken up by simply creating that bitmap object. This is of course making the assumption that your bitmap size isn't constantly changing - if that is the case, then ignore this post - if the bitmap size is (fairly) constant, then keep reading.

class SomeForm : Form
{
  private Bitmap _theImage;
  private bool _working = false;

  void worker_DoWork(object sender, DoWorkEventArgs e)
  {
    // make sure the image is initialized (may as well do it on the background thread).
    if (_theImage == null)
    {
      int width = (int)Math.Round(Width * fDPIX);
      int height = LogHeightPixels;
      _theImage = new Bitmap(width, height/*, System.Drawing.Imaging.PixelFormat.Gdi/*.Format32bppArgb*/);
    }
    // note, you may need to clear the bitmap in this method call.
    // it all depends on what the draw log method is doing.
    DrawLogToBitmap(_theImage);
  }

  void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  {
    _working = false;
    Refresh();
  }

  public override void Draw(Graphics gfx, Rectangle rect)
  {
    gfx.ScaleTransform(zoom, zoom);

    // Update the bitmap (only as needed)
    if (bDirty)
    {
       // I'm assuming this method starts the worker thread - if so:
       _working = true;
       UpdateBitmap(gfx);
    }

    if (_working || worker.IsBusy)
    {
       gfx.DrawString("Updating Bitmap, please wait!", this.Font, Brushes.Black, new PointF(0, 0));
       return;
    }

    RectangleF rcSource = new RectangleF(-horizontalOffset, -verticalOffset, ClientRectangle.Width/zoom, ClientRectangle.Height/zoom);
    RectangleF rcDest = new RectangleF(ClientRectangle.Location.X, ClientRectangle.Location.Y, ClientRectangle.Size.Width/zoom, ClientRectangle.Size.Height/zoom);

    try
    {
       gfx.DrawImage(_theImage, rcDest, rcSource, GraphicsUnit.Pixel);
    }
    catch (Exception exc)
    {
       gfx.DrawString(exc.Message, Font, Brushes.Black, new PointF(0,0));
    }
    etc....
  }
}


I may have missed something (I've been up for FAR too long, and can't sleep for some reason), but that's the gist of it. You'll probably want to do a few more checks to make sure the bitmap isn't being accessed from multiple threads at the same time. This should get rid of your memory exception, and give a significant performance boost, since the garbage collector won't need to run as often and you're not allocating massive amounts of memory all the time.

Another thing you may want to get rid of is that background worker, and just use a normal thread with a longer lifetime. Spawning a new thread for each request does add a bit of overhead, so in similar situations I typically start up a background thread early in the program, and use an AutoResetEvent or similar to tell it when to wake up and do something.

-----
In the land of the blind, the one eyed man is king.

QuestionExtracting specific image from tiff... Pin
PandemoniumPasha8-Mar-07 22:27
PandemoniumPasha8-Mar-07 22:27 
AnswerRe: Extracting specific image from tiff... Pin
PandemoniumPasha9-Mar-07 0:44
PandemoniumPasha9-Mar-07 0:44 
GeneralRe: Extracting specific image from tiff... Pin
Mogtabam10-Mar-07 21:07
Mogtabam10-Mar-07 21:07 
GeneralRe: Extracting specific image from tiff... Pin
Mark Salsbery11-Mar-07 9:16
Mark Salsbery11-Mar-07 9:16 
QuestionManaged DirectX DeviceResizing Pin
Dmitri Nеstеruk6-Mar-07 4:07
Dmitri Nеstеruk6-Mar-07 4:07 
QuestionMax Rectangles In Any Shape Pin
zbaum002-Mar-07 11:49
zbaum002-Mar-07 11:49 
AnswerRe: Max Rectangles In Any Shape Pin
John R. Shaw3-Mar-07 0:36
John R. Shaw3-Mar-07 0:36 
GeneralRe: Max Rectangles In Any Shape Pin
zbaum004-Mar-07 10:38
zbaum004-Mar-07 10:38 
GeneralRe: Max Rectangles In Any Shape Pin
John R. Shaw9-Mar-07 8:35
John R. Shaw9-Mar-07 8:35 
AnswerRe: Max Rectangles In Any Shape Pin
Shog99-Mar-07 11:21
sitebuilderShog99-Mar-07 11:21 
QuestionHow to Connect DV Video Encoder to FileWriter Pin
Brooks Harris2-Mar-07 11:18
Brooks Harris2-Mar-07 11:18 
AnswerRe: How to Connect DV Video Encoder to FileWriter Pin
Mark Salsbery2-Mar-07 12:52
Mark Salsbery2-Mar-07 12:52 
GeneralRe: How to Connect DV Video Encoder to FileWriter Pin
Brooks Harris3-Mar-07 3:58
Brooks Harris3-Mar-07 3:58 
QuestionGDI+ Region::IsVisible() Pin
Mark Salsbery2-Mar-07 8:23
Mark Salsbery2-Mar-07 8:23 
AnswerRe: GDI+ Region::IsVisible() Pin
cmk2-Mar-07 10:44
cmk2-Mar-07 10:44 
GeneralRe: GDI+ Region::IsVisible() Pin
Mark Salsbery2-Mar-07 10:49
Mark Salsbery2-Mar-07 10:49 
GeneralRe: GDI+ Region::IsVisible() Pin
cmk2-Mar-07 10:56
cmk2-Mar-07 10:56 

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.