MCPcopy Create free account
hub / github.com/lxn/walk / newBitmap

Function newBitmap

bitmap.go:78–118  ·  view source on GitHub ↗

newBitmap creates a bitmap with given size in native pixels and DPI.

(size Size, transparent bool, dpi int)

Source from the content-addressed store, hash-verified

76
77// newBitmap creates a bitmap with given size in native pixels and DPI.
78func 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//

Callers 4

NewBitmapFunction · 0.85
NewBitmapForDPIFunction · 0.85

Calls 3

withCompatibleDCFunction · 0.85
newErrorFunction · 0.85
newBitmapFromHBITMAPFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…