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 (c *Canvas) Dispose() { if !c.doNotDispose && c.hdc != 0 { if c.hwnd == 0 { win.DeleteDC(c.hdc) } else { win.ReleaseDC(c.hwnd, c.hdc) } c.hdc = 0 } if c.recordingMetafile != nil { c.recordingMetafile.ensureFinished() c.recordingMetafile = nil } if c.measureTextMetafile != nil { c.measureTextMetafile.Dispose() c.measureTextMetafile = nil } }
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") }