1

我已将问题的原因隔离为图像,因为代码似乎可以与其他具有透明度的 png 图像一起使用。但是,它似乎不适用于我需要的一张图像。当我正在尝试制作一个形状漂亮的窗户时,这将很有帮助。

图片:

替代文字

编码:

import wx

class PictureWindow(wx.Frame):
    def __init__(self, parent, id, title, pic_location):

        # For PNGs. Must be PNG-8 for transparency...
        self.bmp = wx.Image(pic_location, wx.BITMAP_TYPE_PNG).ConvertToBitmap()

        framesize = (self.bmp.GetWidth(), self.bmp.GetHeight())

        # Launch a frame the size of our image. Note the position and style stuff...
        # (Set pos to (-1, -1) to let the OS place it.
        # This style wx.FRAME_SHAPED is a frameless plain window.
        wx.Frame.__init__(self, parent, id, title, size=framesize, pos = (50, 50), style = wx.FRAME_SHAPED)

        r = wx.RegionFromBitmap(self.bmp)
        self.SetShape(r)

        # Define the panel and place the pic
        panel = wx.Panel(self, -1)
        self.mainPic = wx.StaticBitmap(panel, -1, self.bmp)

        # Set an icon for the window if we'd like
        #icon1 = wx.Icon("icon.ico", wx.BITMAP_TYPE_ICO)
        #self.SetIcon(icon1)

        self.Show()

        # The paint stuff is only necessary if doing a shaped window
        self.Bind(wx.EVT_PAINT, self.OnPaint)
        self.Main()


    def OnPaint(self, event):
        dc = wx.PaintDC(self)
        dc.DrawBitmap(self.bmp, 0, 0, True)

    def Main(self):
        sizer = wx.GridBagSizer()
        button = wx.Button(self,-1,label="Click me !")
        sizer.Add(button, (0,1))


# What pic are we opening?
pic_location = r"C:\Users\user\Pictures\CPUBAR\A4.png" # PNG must be png-8 for             transparency...

app = wx.App(redirect=0) # the redirect parameter keeps stdout from opening a wx gui window
PictureWindow(None, -1, 'Picture Viewer', pic_location)
app.MainLoop()

这是在 Windows 7 中,顺便说一句。

4

4 回答 4

7

wx.RegionFromBitmap 使用位图的掩码来设置形状。如果您的源图像有一个 alpha 通道,那么它将没有蒙版,因此 wx.RegionFromBitmap 将无法确定要使用的形状。您可以转换源图像,使所有像素完全不透明或完全透明,然后 wx.Image 将使用蒙版而不是 alpha 通道加载它。或者,您可以在运行时使用 wx.Image 的 ConvertAlphaToMask 方法对其进行转换,然后再将其转换为 wx.Bitmap。

于 2010-07-19T16:06:43.797 回答
0

您的代码说它需要采用 png-8 格式才能使透明度起作用。首先,图片是png-8格式的吗?第二,为什么这是透明度的必要条件???

于 2010-07-19T07:56:31.777 回答
0

您正在将图像转换为位图 - 位图不支持透明度。

于 2010-07-19T13:05:55.107 回答
0

作为一种解决方法,您可以使用 .gif(如果您能忍受有限的颜色集)。它们仅支持 1 位 Alpha 通道。

于 2011-01-24T22:42:11.973 回答