2016-04-06 11 views
1

Ich versuche, einen Text auf vorhandene Bitmap zu zeichnen, aber wenn ich die DrawText-Methode des Graphics Context verwende, wird der Hintergrund entfernt. Aber das passiert nur, wenn ich ein Hintergrundbild aus einer leeren Bitmap erzeuge (die Verwendung von DrawText auf Bitmap aus dem geladenen Bild funktioniert gut). Ich denke, dass das Problem auftritt, weil ich MemoryDC verwende, um eine leere Bitmap zu erstellen, aber ich bin ziemlich neu in WxPython, also habe ich keine Ahnung, wie es zu beheben.wxPython - GC.DrawText entfernt Hintergrund-Bitmap

Hier ist, was ich bisher getan habe:

import wx 

def GetEmptyBitmap(w, h, color=(0,0,0)): 
    """ 
    Create monochromatic bitmap with desired background color. 
    Default is black 
    """ 
    b = wx.EmptyBitmap(w, h) 
    dc = wx.MemoryDC(b) 
    dc.SetBrush(wx.Brush(color)) 
    dc.DrawRectangle(0, 0, w, h) 
    return b 

def drawTextOverBitmap(bitmap, text='', fontcolor=(255, 255, 255)): 
    """ 
    Places text on the center of bitmap and returns modified bitmap. 
    Fontcolor can be set as well (white default) 
    """ 
    dc = wx.MemoryDC(bitmap) 
    gc = wx.GraphicsContext.Create(dc) 
    font = wx.Font(16, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL) 
    gc.SetFont(font, fontcolor) 
    w,h = dc.GetSize() 
    tw, th = dc.GetTextExtent(text)  
    gc.DrawText(text, (w - tw)/2, (h - th)/2) 
    return bitmap 

app = wx.App() 
bmp_from_img = bmp = wx.Image(location).Rescale(200, 100).ConvertToBitmap() 
bmp_from_img = drawTextOverBitmap(bmp_from_img, "From Image", (255,255,255)) 

bmp_from_empty = GetEmptyBitmap(200, 100, (255,0,0)) 
bmp_from_empty = drawTextOverBitmap(bmp_from_empty, "From Empty", (255,255,255)) 


frame = wx.Frame(None) 
st1 = wx.StaticBitmap(frame, -1, bmp_from_img, (0,0), (200,100)) 
st2 = wx.StaticBitmap(frame, -1, bmp_from_empty, (0, 100), (200, 100)) 
frame.Show() 
app.MainLoop() 

Wie gesagt, die StaticBitmap, die das geladene Bild verwendet wird korrekt angezeigt, aber die mit EmptyBitmap erstellt hat keinen Hintergrund.

Haben Sie irgendwelche Ideen, wie es funktioniert?

Danke

Antwort

1

Dieses wie ein Bug mir scheint. Verwenden Sie die folgenden, damit es funktioniert:

def GetEmptyBitmap(w, h, color=(0,0,0)): 
    # ... 
    # instead of 
    # b = wx.EmptyBitmap(w, h) 
    # use the following: 
    img = wx.EmptyImage(w, h) 
    b = img.ConvertFromBitmap() 
    # ... 

Ich denke nicht das wx.MemoryDC schuld ist, aber die plattformspezifischen Bitmap Erstellung Routinen, wo es mehr los unter der Haube. Wenn man mit einer wx.Image beginnt, scheint die Ausgabe vorhersehbarer/nützlicher zu sein.

+0

Danke, das hat das Problem gelöst. Während ich nach der Lösung gesucht habe, wurde mir gesagt, dass dies durch einen Alpha-Fehler auf Null verursacht werden könnte. –

+0

Ich konnte nicht beweisen, dass es etwas mit Alpha zu tun hat, aber es scheint tatsächlich das Problem zu sein. Probieren Sie Folgendes aus: '' b = wx.EmptyBitmapRGBA (w, h, * Farbe, Alpha = 255) '', '' img = b.ConvertToImage() '', '' b = img.ConvertToBitmap() ''. Hin und her über '' wx.Image'' wird der Alpha-Kanal ausgegeben und dann funktioniert es. – nepix32

+0

[Tim Roberts auf Google Groups WxPython-Benutzer] (https://groups.google.com/forum/#!topic/wxpython-users/huyH0pKklks) hat das gleiche herausgefunden: Die Bitmap ohne Alpha-Kanal macht das Ting Arbeit. – nepix32