Clearing BitmapData

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I want to clear all of the byes that are associated with a BitmapData object.
I know that one technique would be to do the following:

'...
Dim bitmapData As Drawing.Imaging.BitmapData = myBitmap.LockBits(rect,
flags, format)
'...
Dim arrayLength As Integer = (bitmapData.Stride * bitmapData.Height)
Dim emptyByteArray(arrayLength - 1) As Byte
System.Runtime.InteropServices.Marshal.Copy(emptyByteArray, 0,
bitmapData.Scan0, emptyByteArray.Length)

The problem is that creating emptyByteArray can require a significant amount
of time and memory when working with large images. Is there a technique that
would not require the creation of emptyByteArray?

Thanks,
Lance
 
Have you tried

Dim gr As Graphics = Graphics.FromImage(DirectCast( _
myBitmap, Image))
gr.Clear(Color.FromArgb(0, 0, 0, 0))
gr.Dispose()

This will set RGB and alpha of all the pixels to 0. You can use
Color.Transparent for the same visual effect but the RGB will be {255, 255,
255} with the alpha 0.

Robby
 
Thanks for the idea, but that won't work because I need to have control over
which region of the image is being cleared. What I'm looking for is a
technique that will allow me to clear a specified number of bytes, starting
at bitmapData.Scan0.

Lance
 
Thanks for the idea. That is the type of thing that I'm looking for.
Unfortunately, using WriteByte is ~5x slower than my sample technique
(presumably due to having to call WriteByte a large number of times). Even
using WriteInt32 is ~2x slower. I think I need a method that allows me to
specify the length to clear, similar to System.Array.Clear. Any other ideas?

Thanks again,
Lance
 
Can the byte region you want to clear be represented by a visable region in
the bitmap? If it can then you can use a method similar to the one I
mentioned previously. However, you will have to set up a clipping region on
the Graphics object if it is an irregular shape or simply create a "zero"
brush and paint the region with it in Fill<xxx> where <xxx> is the shape of
the region.

Robby
 
Hi

I think there is no other managed method to do the job.
Anyway, I think you may try the unmanamged method via calling into the
ZeroMemory API.
Dim myBitMap As New Bitmap("c:\temp\flower.jpg")
<DllImport("kernel32.dll", EntryPoint:="RtlZeroMemory")> _
Public Shared Sub ZeroMemory(ByVal Destination As IntPtr, ByVal Length
As Integer)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim bitmapData As Drawing.Imaging.BitmapData =
myBitMap.LockBits(New Rectangle(0, 0, myBitMap.Width, myBitMap.Height),
Imaging.ImageLockMode.ReadWrite, Imaging.PixelFormat.Format32bppArgb)
Dim arrayLength As Integer = (bitmapData.Stride * bitmapData.Height
/ 4)
ZeroMemory(bitmapData.Scan0, arrayLength)
myBitMap.UnlockBits(bitmapData)
Me.PictureBox1.Refresh()
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.PictureBox1.Image = myBitMap
End Sub


Best regards,

Perter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top