protected override void OnPaint(PaintEventArgs pe) { //these settings aren't even needed for good perf, but they are helpful pe.Graphics.InterpolationMode = InterpolationMode.Low; pe.Graphics.CompositingQuality = CompositingQuality.HighSpeed; pe.Graphics.SmoothingMode = SmoothingMode.HighSpeed; if (image != null) { //draw empty area, if it exists, in an optimized way. if (AutoScrollPosition.X == 0) { int emptyRightAreaWidth = Width - image.Bitmap.Width; if (emptyRightAreaWidth > 0) { Rectangle fillRect = new Rectangle(image.Bitmap.Width, 0, emptyRightAreaWidth, Height); fillRect.Intersect(pe.ClipRectangle); pe.Graphics.FillRectangle(SystemBrushes.Control, fillRect); } } if (AutoScrollPosition.Y == 0) { int emptyRightAreaHeight = Height - image.Bitmap.Height; if (emptyRightAreaHeight > 0) { Rectangle fillRect = new Rectangle(0, image.Bitmap.Height, Width, emptyRightAreaHeight); fillRect.Intersect(pe.ClipRectangle); pe.Graphics.FillRectangle(SystemBrushes.Control, fillRect); } } //calculate the visible area of the bitmap Rectangle bitmapRect = new Rectangle(AutoScrollPosition.X, AutoScrollPosition.Y, image.Bitmap.Width, image.Bitmap.Height); Rectangle visibleClientRect = bitmapRect; visibleClientRect.Intersect(pe.ClipRectangle); if (visibleClientRect.Width == 0 || visibleClientRect.Height == 0) { return; } Rectangle visibleBitmapRect = visibleClientRect; visibleBitmapRect.Offset(-AutoScrollPosition.X, -AutoScrollPosition.Y); //draw bitmap if (newMethod) { using (EditableBitmap section = image.CreateView(visibleBitmapRect)) pe.Graphics.DrawImageUnscaled(section.Bitmap, visibleClientRect.Location); } else //normal method { pe.Graphics.DrawImage(image.Bitmap, visibleClientRect, visibleBitmapRect, GraphicsUnit.Pixel); } } else //if no bitmap just fill with background color { pe.Graphics.FillRectangle(SystemBrushes.Control, pe.ClipRectangle); } }
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(); } } }