Howdy people,
I have recently been experimenting with the Bitmap.SetPixel function. While it does work fine it's incredibly slow and I was interested in faster methods to edit Bitmaps pixel by pixel.
I found this class which allows easy editing of bytes in bitmaps and added the setpixel function. Lock the image, set the pixels, unlock it ... Simple bytes copying with arrays.
However, when put in the paint method, here's and example:
This works fine and does set the pixels for the bitmap, but if you put a timer and invalidate the form every 10 ms or even less than that, the garbage collector goes crazy like if i allocated a huge amount of memory each time without clearing it.
I don't know what causes that to happen ...
Thanks in advance
I have recently been experimenting with the Bitmap.SetPixel function. While it does work fine it's incredibly slow and I was interested in faster methods to edit Bitmaps pixel by pixel.
I found this class which allows easy editing of bytes in bitmaps and added the setpixel function. Lock the image, set the pixels, unlock it ... Simple bytes copying with arrays.
vb.net Code:
Option Strict On Imports System.Drawing.Imaging Imports System.Runtime.InteropServices Public Class BmpControl Public ImageBytes() As Byte Private Bmp As Bitmap Private BmpData As BitmapData ' Save a reference to the bitmap. Public Sub New(ByVal BitmapImage As Bitmap) Bmp = BitmapImage End Sub Public Sub LockBitmap() ' Lock the bitmap data. Dim bounds As Rectangle = New Rectangle( 0, 0, Bmp.Width, Bmp.Height) BmpData = Bmp.LockBits(bounds, Imaging.ImageLockMode.ReadWrite, Imaging.PixelFormat.Format32bppArgb) ' Allocate room for the data. Dim total_size As Integer = BmpData.Stride * BmpData.Height ReDim ImageBytes(total_size) ' Copy the data into the ImageBytes array. Marshal.Copy(BmpData.Scan0, ImageBytes, 0, total_size) End Sub Public Sub UnlockBitmap() ' Copy the data back into the bitmap. Dim total_size As Integer = BmpData.Stride * BmpData.Height Marshal.Copy(ImageBytes, 0, BmpData.Scan0, total_size) ' Unlock the bitmap. Bmp.UnlockBits(BmpData) ' Release resources. ImageBytes = Nothing BmpData = Nothing End Sub Public Sub SetPixel(ByVal Pixel As Point, ByVal PixelColor As Color) Dim ByteLocation As Integer = ((Pixel.Y * BmpData.Stride) + (Pixel.X * 4)) ImageBytes(ByteLocation) = PixelColor.A ImageBytes(ByteLocation + 1) = PixelColor.R ImageBytes(ByteLocation + 2) = PixelColor.G ImageBytes(ByteLocation + 3) = PixelColor.B End Sub End Class
However, when put in the paint method, here's and example:
vb.net Code:
Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint Dim BitmapCtrl As New BmpControl(Mybitmap) BitmapCtrl.LockBitmap() For I = 13 To 533 BitmapCtrl.SetPixel(New Point(I, 8), Color.Blue) Next BitmapCtrl.UnlockBitmap() Dim G As Graphics = e.Graphics G.DrawImage(Mybitmap, New Point(0, 0)) End Sub
This works fine and does set the pixels for the bitmap, but if you put a timer and invalidate the form every 10 ms or even less than that, the garbage collector goes crazy like if i allocated a huge amount of memory each time without clearing it.
I don't know what causes that to happen ...
Thanks in advance