I noticed my program was leaking memory. So I used dotMemory to find the leak, and looks like this is the function causing the leak:
private void LoadBits()
{
// Lock the bitmap's bits.
Rectangle rect = new Rectangle(0, 0, bm.Width, bm.Height);
bmpData = bm.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bm.PixelFormat);
stride = bmpData.Stride;
// Get the address of the first line.
IntPtr ptr = bmpData.Scan0;
// Declare an array to hold the bytes of the bitmap.
byteCount = Math.Abs(bmpData.Stride) * bm.Height;
bytes = new byte[byteCount];
// Copy the RGB values into the array.
System.Runtime.InteropServices.Marshal.Copy(ptr, bytes, 0, byteCount);
}
And this how I unlock the bits.
private void SaveBits()
{
// Update Stuff
IntPtr ptr = bmpData.Scan0;
System.Runtime.InteropServices.Marshal.Copy(bytes, 0, ptr, byteCount);
bm.UnlockBits(bmpData);
}
I implemented the IDisposable interface for this class. And I call the SaveBits there, so even if I forget to call SaveBits, the GC should do it for me.
And Yes, I do call bm.Dispose() and set everything to null in the Dispose method.
You need to UnlockBits() when you're done.
Related
I am trying to create an image surface in c# CairoSharp using these two constructors:
public ImageSurface(byte[] data, Format format, int width, int height, int stride); public ImageSurface(IntPtr data, Format format, int width, int height, int stride);
I am trying to get the array of the linux framebuffer from a memorymappedfile:
var file = MemoryMappedFile.CreateFromFile("/dev/fb0", FileMode.Open, null, (3840 * 2160 * (32 / 8)));
I know I have to use an unsafe context to get it but am unsure the proper syntax to get the sequential pointer from the memeoryMapped object.
The constructors for the ImageSurface will not work with the MemoryMappedFile directly. You will have to Read bytes from the MemoryMappedFile and use those bytes to create the ImageSurface.
I never used C# on Linux before so I don't really know if all those objects are available but maybe like this?
private static void test()
{
Bitmap bmp = (Bitmap)Image.FromFile("some image");
BitmapData imgData = null;
try
{
imgData = bmp.LockBits(
new Rectangle(0, 0, bmp.Width, bmp.Height),
ImageLockMode.ReadWrite,
bmp.PixelFormat
);
int finalLength = imgData.Stride * imgData.Height;
byte[] buf = new byte[finalLength];
IntPtr ptr = imgData.Scan0;
System.Runtime.InteropServices.Marshal.Copy(ptr, buf, 0, finalLength);
bmp.UnlockBits(imgData);
// Pointer to first byte lives inside of fixed context.
unsafe
{
fixed (byte* p = buf)
{
// your cairo code...
}
}
// Alternative...
var ptPinned = System.Runtime.InteropServices.GCHandle.Alloc(
buf, System.Runtime.InteropServices.GCHandleType.Pinned);
IntPtr ptCairo = ptPinned.AddrOfPinnedObject();
ptPinned.Free();
}
finally
{
if (imgData != null) {
bmp.UnlockBits(imgData);
}
}
}
In any case I am certain that you have to pass the pointer of an already allocated buffer. In the test above I just loaded the image pixels of a bitmap into a byte array. In order to get the pointer you Marshal it or use fixed. That is all on Windows though.
i'm trying to do method that should return difference between two images in byte array(rgb values) and send it via UDP. Code: (there's no part with sending this array because it doesn't matter now)
public void getdiff(Bitmap lol,Bitmap lol2)
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
Rectangle rect = new Rectangle(0, 0, lol.Width, lol.Height);
BitmapData bmpData = lol.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
IntPtr ptr = bmpData.Scan0;
Rectangle rect2 = new Rectangle(0, 0, lol2.Width, lol2.Height);
BitmapData bmpData2 = lol2.LockBits(rect2, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
IntPtr ptr2 = bmpData2.Scan0;
int numBytes = bmpData.Stride * lol.Height;
byte[] rgbValues = new byte[numBytes];
int numBytes2 = bmpData2.Stride * lol2.Height;
byte[] rgbValues2 = new byte[numBytes2];
byte[] difference = new byte[numBytes];
Marshal.Copy(ptr, rgbValues, 0, numBytes);
Marshal.Copy(ptr2, rgbValues2, 0, numBytes2);
for (int counter = 0; counter < rgbValues.Length; counter++)
{
if (rgbValues[counter] != rgbValues2[counter])
{
difference[counter] = rgbValues[counter];
}
}
Marshal.Copy(rgbValues, 0, ptr, numBytes);
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
textBox1.Text = ts.Milliseconds.ToString();
lol.UnlockBits(bmpData);
lol2.UnlockBits(bmpData2);
}
Right now I have 2 byte arrays from 2 images, and I just compare them, when images are different, I write adequate RGB values from chosen image to difference[]. Problem is, when RGB values are equal in both images, adequate places in this difference array are filled with 0(triple 0 represent White colour). So problem is, if i would "impose" this difference[] on to target image it will be probably mostly white. I really want to use Marshal.Copy and Lockbits because it's really efficient.
Question is: How can I store this image difference to avoid for multiplying "for" loops at reading/imposing it? Maybe I'm missing some methods? I would like to stay with LockBits and Marshal.Copy, but if you have better ideas - please share with me.
What is the difference between System.Drawing.bitmap and System.Drawing.Imaging.bitmapdata in C#?
How to convert them to each other?
System.Drawing.Bitmap is an actual bitmap object. You can use it to draw to using a Graphics instance obtained from it, you can display it on the screen, you can save the data to a file, etc.
The System.Drawing.Imaging.BitmapData class is a helper object used when calling the Bitmap.LockBits() method. It contains information about the locked bitmap, which you can use to inspect the pixel data within the bitmap.
You can't really "convert" between the two per se, as they don't represent the same information. You can obtain a BitmapData object from a Bitmap object simply by calling LockBits(). If you have a BitmapData object from some other Bitmap object, you can copy that data to a new Bitmap object by allocating one with the same format as the original, calling LockBits on that one too, and then just copying the bytes from one to the other.
Convert bitmap to bitmap data. Also refer this link
Private void LockUnlockBitsExample(PaintEventArgs e) {
// Create a new bitmap.
Bitmap bmp = new Bitmap("c:\\fakePhoto.jpg");
// Lock the bitmap's bits.
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat);
// Get the address of the first line.
IntPtr ptr = bmpData.Scan0;
// Declare an array to hold the bytes of the bitmap.
int bytes = Math.Abs(bmpData.Stride) * bmp.Height;
byte[] rgbValues = new byte[bytes];
// Copy the RGB values into the array.
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
// Set every third value to 255. A 24bpp bitmap will look red.
for (int counter = 2; counter < rgbValues.Length; counter += 3) rgbValues[counter] = 255;
// Copy the RGB values back to the bitmap
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);
// Unlock the bits.
bmp.UnlockBits(bmpData);
// Draw the modified image.
e.Graphics.DrawImage(bmp, 0, 150);
}
I need to port some OpenGL code to C# OpenTK.
Here is the chunk where I update a mapped PBO from an array of pixels in C++ :
GLubyte* ptr = (GLubyte*)glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);
if(ptr)
{
memcpy(ptr,imageInfo.Data,IMG_DATA_SIZE);
glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
}
I need to do the same in OpenTK.My image data comes from an instance of Bitmap.
I tried the following:
IntPtr ptr = GL.MapBuffer(BufferTarget.PixelUnpackBuffer, BufferAccess.WriteOnly);
if(ptr != IntPtr.Zero)
{
BitmapData data = updateColorMap.LockBits(new System.Drawing.Rectangle(0, 0, updateColorMap.Width, updateColorMap.Height),
ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
Marshal.Copy(data.Scan0, 0, ptr, IMG_DATA_SIZE);
}
But Marshal.Copy requires the first param to be of byte[] type.I didn't find how to retrieve it from the BitmapData.It returns only IntPtr (data.Scan0) .
So how can I get the byte array from the Bitmap?
UPDATE:
In the meantime I got help from the OpenTK forum and they proposed to do this instead:
unsafe
{
GL.BufferData(BufferTarget.PixelUnpackBuffer, new IntPtr(IMG_DATA_SIZE), IntPtr.Zero, BufferUsageHint.StreamDraw);
byte* ptr = (byte*)GL.MapBuffer(BufferTarget.PixelUnpackBuffer, BufferAccess.WriteOnly);
if (ptr != null)
{
BitmapData data = updateDepthMap.LockBits(new System.Drawing.Rectangle(0, 0, updateDepthMap.Width, updateDepthMap.Height),
ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
byte* scan0 = (byte*)data.Scan0.ToPointer();
for (int i = 0; i < IMG_DATA_SIZE; ++i)
{
*ptr = *scan0;
++ptr;
++scan0;
}
updateDepthMap.UnlockBits(data);
GL.UnmapBuffer(BufferTarget.PixelUnpackBuffer);
}
}//unsafe
Now,this works,but it is TERRIBLY SLOW! The regular texture update runs 2x faster than this,which is
wrong as async PBO transfer should speed up texture uploads.Indeed in my C++ version PBO upload causes almost 2x performance boost.
Ok so the solution is here: Copy data from from IntPtr to IntPtr
tested on linux.
I have a C# .NET library that grabs frames from a camera. I need to send those frames to a native application that takes images from an unsigned char*.
I initially take the frames as System::Drawing::Bitmap.
So far I can retrieve a byte[] from the Bitmap. My test is done with an image of resolution 400*234, I should be getting 400*234*3 bytes to get to the 24bpp a RGB image requires.
However, I'm getting a byte[] of size 11948.
This is how I convert from Bitmap to byte[]:
private static byte[] ImageToByte(Bitmap img)
{
ImageConverter converter = new ImageConverter();
return (byte[])converter.ConvertTo(img, typeof(byte[]));
}
What is the proper way to convert from System::Drawing::Bitmap to RGB unsigned char*?
This has to be done using the lockBits method, here is a code example:
Rectangle rect = new Rectangle(0, 0, m_bitmap.Width, m_bitmap.Height);
BitmapData bmpData = m_bitmap.LockBits(rect, ImageLockMode.ReadOnly,
m_bitmap.PixelFormat);
IntPtr ptr = bmpData.Scan0;
int bytes = Math.Abs(bmpData.Stride) * m_bitmap.Height;
byte[] rgbValues = new byte[bytes];
Marshal.Copy(ptr, rgbValues, 0, bytes);
m_bitmap.UnlockBits(bmpData);
GCHandle handle = GCHandle::Alloc(rgbValues, GCHandleType::Pinned);
unsigned char * data = (unsigned char*) (void*) handle.AddrOfPinnedObject();
//do whatever with data