func withCompatibleDC(f func(hdc win.HDC) error) error { hdc := win.CreateCompatibleDC(0) if hdc == 0 { return newError("CreateCompatibleDC failed") } defer win.DeleteDC(hdc) return f(hdc) }
func NewCanvasFromImage(image Image) (*Canvas, error) { switch img := image.(type) { case *Bitmap: hdc := win.CreateCompatibleDC(0) if hdc == 0 { return nil, newError("CreateCompatibleDC failed") } succeeded := false defer func() { if !succeeded { win.DeleteDC(hdc) } }() if win.SelectObject(hdc, win.HGDIOBJ(img.hBmp)) == 0 { return nil, newError("SelectObject failed") } succeeded = true return (&Canvas{hdc: hdc}).init() case *Metafile: c, err := newCanvasFromHDC(img.hdc) if err != nil { return nil, err } c.recordingMetafile = img return c, nil } return nil, newError("unsupported image type") }