diff --git a/base/bmp_io_c.h b/base/bmp_io_c.h index 97d9bf2..6812ae8 100644 --- a/base/bmp_io_c.h +++ b/base/bmp_io_c.h @@ -320,6 +320,16 @@ int saveMMBitmapAsBMP(MMBitmapRef bitmap, const char *path) return 0; } +uint8_t *saveMMBitmapAsBytes(MMBitmapRef bitmap, size_t *dataLen) +{ + uint8_t *data; + if ((data = createBitmapData(bitmap, dataLen)) == NULL) { + *dataLen = -1; + return NULL; + } + return data; +} + static uint8_t *readImageData(FILE *fp, size_t width, size_t height, uint8_t bytesPerPixel, size_t bytewidth) { diff --git a/robotgo.go b/robotgo.go index 6ff3f4a..f337837 100644 --- a/robotgo.go +++ b/robotgo.go @@ -797,6 +797,19 @@ func ToCBitmap(bit Bitmap) C.MMBitmapRef { return cbitmap } +// ToBitmapBytes saves Bitmap to bitmap format in bytes +func ToBitmapBytes(bit C.MMBitmapRef) []byte { + var len C.size_t + ptr := C.saveMMBitmapAsBytes(bit, &len) + if int(len) < 0 { + return nil + } + bs := C.GoBytes(unsafe.Pointer(ptr), C.int(len)) + C.free(unsafe.Pointer(ptr)) + return bs +} + + // ToMMBitmapRef trans CBitmap to C.MMBitmapRef func ToMMBitmapRef(bit CBitmap) C.MMBitmapRef { return C.MMBitmapRef(bit)