private BitmapInfo PrepareBitmap(int id, int width, int height) { if (!m_bmp_cache.ContainsKey(id)) { EditableBitmap bmp = new EditableBitmap(width, height, PixelFormat.Format24bppRgb); Graphics gfx = Graphics.FromImage(bmp.Bitmap); SetupGraphics(gfx); gfx.Clear(m_canvas_color); BitmapInfo bmp_info = new BitmapInfo(bmp, gfx); m_bmp_cache.Add(id, bmp_info); return(bmp_info); } else { BitmapInfo bmp_info = m_bmp_cache[id]; if (bmp_info.Bitmap.Width >= width && bmp_info.Bitmap.Height >= height) { bmp_info.Graphics.Clip = new Region(new Rectangle(0, 0, width, height)); bmp_info.Graphics.Clear(m_canvas_color); return(bmp_info); } // remove old bitmap bmp_info.Dispose(); m_bmp_cache.Remove(id); // create new bitmap EditableBitmap bmp = new EditableBitmap(width, height, PixelFormat.Format24bppRgb); Graphics gfx = Graphics.FromImage(bmp.Bitmap); SetupGraphics(gfx); gfx.Clear(m_canvas_color); bmp_info = new BitmapInfo(bmp, gfx); m_bmp_cache.Add(id, bmp_info); return(bmp_info); } }
// *********************** public void Draw(BoundingArea.ReadOnly bounding_area, params IDrawableObject[] drawable_objects) { Utils.ThrowException(bounding_area == null ? new ArgumentNullException("bounding_area") : null); Utils.ThrowException(drawable_objects == null ? new ArgumentNullException("drawable_objects") : null); DateTime start_time = DateTime.Now; BoundingArea extended_area = bounding_area.GetWritableCopy(); ExtendBoundingArea(extended_area, drawable_objects); #if !NO_BB_SIMPLIFICATION extended_area.Optimize(); #endif TransformParams tr = new TransformParams(0, 0, m_scale_factor); Set <IDrawableObject> outdated_objects = new Set <IDrawableObject>(drawable_objects); drawable_objects = m_drawable_object.GetObjectsIn(extended_area, tr); Rectangle enclosing_rect = GetEnclosingRect(extended_area.BoundingBox); BitmapInfo render_layer = PrepareBitmap(RENDER_LAYER, enclosing_rect.Width, enclosing_rect.Height); TransformParams render_tr = new TransformParams(-enclosing_rect.X, -enclosing_rect.Y, m_scale_factor); BoundingArea extended_area_tr = extended_area.Clone(); extended_area_tr.Transform(new TransformParams(-enclosing_rect.X, -enclosing_rect.Y, 1)); for (int i = drawable_objects.Length - 1; i >= 0; i--) { if (outdated_objects.Contains(drawable_objects[i])) { drawable_objects[i].Draw(render_layer.Graphics, render_tr); } else { drawable_objects[i].Draw(render_layer.Graphics, render_tr, extended_area_tr); } } BitmapInfo main_layer = m_bmp_cache[MAIN_LAYER]; Graphics canvas_gfx = Graphics.FromHwnd(picBoxCanvas.Handle); foreach (RectangleF rect in extended_area.Rectangles) { Rectangle view_area = GetEnclosingRect(rect); view_area.X -= enclosing_rect.X; view_area.Y -= enclosing_rect.Y; view_area.Intersect(new Rectangle(0, 0, enclosing_rect.Width, enclosing_rect.Height)); EditableBitmap view = render_layer.EditableBitmap.CreateView(view_area); main_layer.Graphics.DrawImageUnscaled(view.Bitmap, view_area.X + enclosing_rect.X, view_area.Y + enclosing_rect.Y); // clipping to visible area?!? canvas_gfx.DrawImageUnscaled(view.Bitmap, view_area.X + enclosing_rect.X, view_area.Y + enclosing_rect.Y); //view_on_view.Dispose(); view.Dispose(); } canvas_gfx.Dispose(); TimeSpan draw_time = DateTime.Now - start_time; m_draw_time += draw_time; m_draw_count++; FpsInfo.Text = string.Format("{0:0.00} ms / draw", (double)m_draw_time.TotalMilliseconds / (double)m_draw_count); m_draw_info.Add(draw_time.TotalMilliseconds); FpsInfo.Refresh(); }
private void SetupCanvas(int width, int height) { if (canvas_view != null) { canvas_view.Dispose(); } BitmapInfo main_layer = m_bmp_cache[MAIN_LAYER]; canvas_view = main_layer.EditableBitmap.CreateView(new Rectangle(0, 0, width, height)); }
protected void Dispose(bool disposing) { if (disposed) { return; } bitmap.Dispose(); byteArray.ReleaseReference(); disposed = true; //Set managed object refs to null if explicitly disposing, so that they can be cleaned up by the GC. if (disposing) { owner = null; bitmap = null; } }
/// <summary> /// Creates an <see cref="EditableBitmap"/> as a view on a section of an existing <see cref="EditableBitmap"/>. /// </summary> /// <param name="source"></param> /// <param name="viewArea"></param> protected EditableBitmap(EditableBitmap source, Rectangle viewArea) { owner = source; pixelFormatSize = source.pixelFormatSize; byteArray = source.byteArray; byteArray.AddReference(); stride = source.stride; try { startOffset = source.startOffset + (stride * viewArea.Y) + (viewArea.X * pixelFormatSize); bitmap = new Bitmap(viewArea.Width, viewArea.Height, stride, source.Bitmap.PixelFormat, (IntPtr)(((int)byteArray.bitPtr) + startOffset)); } finally { if (bitmap == null) { byteArray.ReleaseReference(); } } }
public BitmapInfo(EditableBitmap bmp, Graphics gfx) { EditableBitmap = bmp; Graphics = gfx; }
/// <summary> /// Creates an <see cref="EditableBitmap"/> as a view on a section of an existing <see cref="EditableBitmap"/>. /// </summary> /// <param name="source"></param> /// <param name="viewArea"></param> protected EditableBitmap(EditableBitmap source, Rectangle viewArea) { owner=source; pixelFormatSize=source.pixelFormatSize; byteArray=source.byteArray; byteArray.AddReference(); stride = source.stride; try { startOffset=source.startOffset+(stride*viewArea.Y)+(viewArea.X*pixelFormatSize); bitmap = new Bitmap(viewArea.Width, viewArea.Height, stride, source.Bitmap.PixelFormat, (IntPtr)(((long)byteArray.bitPtr)+startOffset)); } finally { if(bitmap==null) byteArray.ReleaseReference(); } }
protected void Dispose(bool disposing) { if(disposed) return; bitmap.Dispose(); byteArray.ReleaseReference(); disposed = true; //Set managed object refs to null if explicitly disposing, so that they can be cleaned up by the GC. if (disposing) { owner=null; bitmap=null; } }