newBitmap creates a bitmap with given size in native pixels and DPI.
(size Size, transparent bool, dpi int)
| 76 | |
| 77 | // newBitmap creates a bitmap with given size in native pixels and DPI. |
| 78 | func newBitmap(size Size, transparent bool, dpi int) (bmp *Bitmap, err error) { |
| 79 | err = withCompatibleDC(func(hdc win.HDC) error { |
| 80 | bufSize := int(size.Width * size.Height * 4) |
| 81 | |
| 82 | var hdr win.BITMAPINFOHEADER |
| 83 | hdr.BiSize = uint32(unsafe.Sizeof(hdr)) |
| 84 | hdr.BiBitCount = 32 |
| 85 | hdr.BiCompression = win.BI_RGB |
| 86 | hdr.BiPlanes = 1 |
| 87 | hdr.BiWidth = int32(size.Width) |
| 88 | hdr.BiHeight = int32(size.Height) |
| 89 | hdr.BiSizeImage = uint32(bufSize) |
| 90 | dpm := int32(math.Round(float64(dpi) * inchesPerMeter)) |
| 91 | hdr.BiXPelsPerMeter = dpm |
| 92 | hdr.BiYPelsPerMeter = dpm |
| 93 | |
| 94 | var bitsPtr unsafe.Pointer |
| 95 | |
| 96 | hBmp := win.CreateDIBSection(hdc, &hdr, win.DIB_RGB_COLORS, &bitsPtr, 0, 0) |
| 97 | switch hBmp { |
| 98 | case 0, win.ERROR_INVALID_PARAMETER: |
| 99 | return newError("CreateDIBSection failed") |
| 100 | } |
| 101 | |
| 102 | if transparent { |
| 103 | win.GdiFlush() |
| 104 | |
| 105 | bits := (*[1 << 24]byte)(bitsPtr) |
| 106 | |
| 107 | for i := 0; i < bufSize; i += 4 { |
| 108 | // Mark pixel as not drawn to by GDI. |
| 109 | bits[i+3] = 0x01 |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | bmp, err = newBitmapFromHBITMAP(hBmp, dpi) |
| 114 | return err |
| 115 | }) |
| 116 | |
| 117 | return |
| 118 | } |
| 119 | |
| 120 | // NewBitmapFromFile creates new bitmap from a bitmap file at 96dpi. |
| 121 | // |
no test coverage detected
searching dependent graphs…