Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15438

VS 2019 Faster Bitmap SetPixel & Memory Management

$
0
0
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.

vb.net Code:
  1. Option Strict On
  2.  
  3. Imports System.Drawing.Imaging
  4. Imports System.Runtime.InteropServices
  5.  
  6. Public Class BmpControl
  7.  
  8.     Public ImageBytes() As Byte
  9.     Private Bmp As Bitmap
  10.     Private BmpData As BitmapData
  11.  
  12.     ' Save a reference to the bitmap.
  13.     Public Sub New(ByVal BitmapImage As Bitmap)
  14.         Bmp = BitmapImage
  15.     End Sub
  16.  
  17.     Public Sub LockBitmap()
  18.         ' Lock the bitmap data.
  19.         Dim bounds As Rectangle = New Rectangle(
  20.             0, 0, Bmp.Width, Bmp.Height)
  21.         BmpData = Bmp.LockBits(bounds,
  22.             Imaging.ImageLockMode.ReadWrite,
  23.             Imaging.PixelFormat.Format32bppArgb)
  24.  
  25.         ' Allocate room for the data.
  26.         Dim total_size As Integer = BmpData.Stride *
  27.             BmpData.Height
  28.         ReDim ImageBytes(total_size)
  29.  
  30.         ' Copy the data into the ImageBytes array.
  31.         Marshal.Copy(BmpData.Scan0, ImageBytes,
  32.             0, total_size)
  33.     End Sub
  34.  
  35.     Public Sub UnlockBitmap()
  36.         ' Copy the data back into the bitmap.
  37.         Dim total_size As Integer = BmpData.Stride * BmpData.Height
  38.         Marshal.Copy(ImageBytes, 0,
  39.             BmpData.Scan0, total_size)
  40.  
  41.         ' Unlock the bitmap.
  42.         Bmp.UnlockBits(BmpData)
  43.  
  44.         ' Release resources.
  45.         ImageBytes = Nothing
  46.         BmpData = Nothing
  47.     End Sub
  48.  
  49.     Public Sub SetPixel(ByVal Pixel As Point, ByVal PixelColor As Color)
  50.         Dim ByteLocation As Integer = ((Pixel.Y * BmpData.Stride) + (Pixel.X * 4))
  51.         ImageBytes(ByteLocation) = PixelColor.A
  52.         ImageBytes(ByteLocation + 1) = PixelColor.R
  53.         ImageBytes(ByteLocation + 2) = PixelColor.G
  54.         ImageBytes(ByteLocation + 3) = PixelColor.B
  55.     End Sub
  56.  
  57. End Class

However, when put in the paint method, here's and example:

vb.net Code:
  1. Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
  2.         Dim BitmapCtrl As New BmpControl(Mybitmap)
  3.         BitmapCtrl.LockBitmap()
  4.         For I = 13 To 533
  5.             BitmapCtrl.SetPixel(New Point(I, 8), Color.Blue)
  6.         Next
  7.         BitmapCtrl.UnlockBitmap()
  8.  
  9.         Dim G As Graphics = e.Graphics
  10.         G.DrawImage(Mybitmap, New Point(0, 0))
  11.     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

Viewing all articles
Browse latest Browse all 15438

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>