MCPcopy Index your code
hub / github.com/lxn/walk / hBitmapFromImage

Function hBitmapFromImage

bitmap.go:523–568  ·  view source on GitHub ↗
(im image.Image, dpi int)

Source from the content-addressed store, hash-verified

521}
522
523func hBitmapFromImage(im image.Image, dpi int) (win.HBITMAP, error) {
524 var bi win.BITMAPV5HEADER
525 bi.BiSize = uint32(unsafe.Sizeof(bi))
526 bi.BiWidth = int32(im.Bounds().Dx())
527 bi.BiHeight = -int32(im.Bounds().Dy())
528 bi.BiPlanes = 1
529 bi.BiBitCount = 32
530 bi.BiCompression = win.BI_BITFIELDS
531 dpm := int32(math.Round(float64(dpi) * inchesPerMeter))
532 bi.BiXPelsPerMeter = dpm
533 bi.BiYPelsPerMeter = dpm
534 // The following mask specification specifies a supported 32 BPP
535 // alpha format for Windows XP.
536 bi.BV4RedMask = 0x00FF0000
537 bi.BV4GreenMask = 0x0000FF00
538 bi.BV4BlueMask = 0x000000FF
539 bi.BV4AlphaMask = 0xFF000000
540
541 hdc := win.GetDC(0)
542 defer win.ReleaseDC(0, hdc)
543
544 var lpBits unsafe.Pointer
545
546 // Create the DIB section with an alpha channel.
547 hBitmap := win.CreateDIBSection(hdc, &bi.BITMAPINFOHEADER, win.DIB_RGB_COLORS, &lpBits, 0, 0)
548 switch hBitmap {
549 case 0, win.ERROR_INVALID_PARAMETER:
550 return 0, newError("CreateDIBSection failed")
551 }
552
553 // Fill the image
554 bitmap_array := (*[1 << 30]byte)(unsafe.Pointer(lpBits))
555 i := 0
556 for y := im.Bounds().Min.Y; y != im.Bounds().Max.Y; y++ {
557 for x := im.Bounds().Min.X; x != im.Bounds().Max.X; x++ {
558 r, g, b, a := im.At(x, y).RGBA()
559 bitmap_array[i+3] = byte(a >> 8)
560 bitmap_array[i+2] = byte(r >> 8)
561 bitmap_array[i+1] = byte(g >> 8)
562 bitmap_array[i+0] = byte(b >> 8)
563 i += 4
564 }
565 }
566
567 return hBitmap, nil
568}
569
570func hBitmapFromWindow(window Window) (win.HBITMAP, error) {
571 hdcMem := win.CreateCompatibleDC(0)

Callers 1

NewBitmapFromImageForDPIFunction · 0.85

Calls 3

newErrorFunction · 0.85
BoundsMethod · 0.65
AtMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…