简体   繁体   中英

Loading bitmap takes too long time

I have a image where I am doing resizing,drawString and FillEllipse .
There are many points(FillEllipse) that needs to shown n bitmap, so I am using for loop.
Here is the code:

using (System.Drawing.Graphics Gfx = System.Drawing.Graphics.FromImage(OrginalBitmap))
{
      Gfx.SmoothingMode = SmoothingMode.HighQuality;
      Gfx.CompositingQuality = CompositingQuality.HighQuality;
      Gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
      Gfx.PixelOffsetMode = PixelOffsetMode.HighQuality;

     foreach (var points in SelectedPoints)
      {
          Gfx.FillEllipse(
              Brushes.Yellow,new Rectangle(points.X , points.Y, 8, 8));
          Gfx.DrawString("M", new Font("Arial",8), 
              Brushes.Yellow, points.X, points.Y); 
          //points.X and points.X are the points that needs to be drawn on bitmap(particular location).
      }      
  }
((IDisposable)OrginalBitmap).Dispose;

Loading of drawn bitmap takes very long time if there are many points in SelectedPoints. Performance had drastically come down and loading takes too much memory. Please let me know what to do.

Thanks in advance.

Drawing just 200 points should really not cause any performance problems, even at the highest quality setting. Using you code, I can draw around 40000 points in one second on my system.

Assuming that SelectedPoints is Point[] or List<Point> or some other efficient type, I would suspect the FontFacade.Large call. Is a new Font instance created each time around?

EDIT:

Running your modified code using new Font("Arial", 8) on 200 points takes around 20 milliseconds on my system, so there have to be something else that is causing your problems. How long does it take to run the code on your system?

Stopwatch timer = Stopwatch.StartNew();
[...]
Debug.WriteLine(timer.ElapsedMilliseconds);

The created font objects should be disposed when done, I would also move it outside the loop so that only one instance is created although that does not seem to be the source of your problems.

using(Font font = new Font("Arial", 8))
{
  foreach(var point = SelectedPoints)
  {
    [...]
  }
}

What dimensions is the OriginalBitmap, and what is it's PixelFormat?

What type is SelectedPoints?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM