【问题标题】:VB.net loop through resources and input into BitmapVB.net 循环遍历资源并输入到 Bitmap
【发布时间】:2014-03-12 03:14:47
【问题描述】:

大家好,我正在尝试找到一种方法来循环浏览我的资源并仅查找标题中包含“GAME”的资源。

我可以使用这段代码很好地做到这一点:

Dim ResourceSet As Resources.ResourceSet = My.Resources.ResourceManager.GetResourceSet(Globalization.CultureInfo.CurrentCulture, True, True)

For Each Dict As DictionaryEntry In ResourceSet.OfType(Of Object)()
    If TypeOf (Dict.Value) Is Drawing.Image Then
        If InStr(Dict.Key, "GAME") <> 0 Then
            Dim newPictureBox As New Button
            Dim s As Stream = Me.GetType().Assembly.GetManifestResourceStream(Dict.Key)
            Dim bmp As New Bitmap(s)
            newPictureBox.BackgroundImage = Image.FromStream(Dict.Value)
            newPictureBox.Tag = Dict.Value
            newPictureBox.Name = "img_" & Dict.Value
            newPictureBox.Width = bmp.Width.ToString()
            newPictureBox.Height = bmp.Height.ToString()

            FlowLayoutPanelNES.Controls.Add(newPictureBox)
            AddHandler newPictureBox.Click, AddressOf newPictureBox_Click

            bmp.Dispose()
        End If
    End If
Next

但是,一旦我尝试将该资源图像放入位图中,就会出现错误:

Value of 'null' is not valid for 'stream'.

上线:

Dim bmp As New Bitmap(s)

为了将它加载到位图以及按钮的 BackgroundImage 中,我会缺少什么?

【问题讨论】:

    标签: vb.net bitmap resources embedded-resource


    【解决方案1】:

    试试:

    Dim bmp As Bitmap = New Bitmap(CType(Dict.Value, Bitmap).Width, CType(Dict.Value, Bitmap).Height, Drawing.Imaging.PixelFormat.Format24bppRgb)
    
    bmp = CType(Dict.Value, Bitmap)
    

    变态

    【讨论】:

    • 似乎不起作用,因为我收到错误 Parameter is not valid. 这是 System.ArgumentException 发生 HResult=-2147024809 Message=Parameter is not valid。 Source=System.Drawing StackTrace: 在 System.Drawing.TextureBrush..ctor(Image image, WrapMode wrapMode) InnerException:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-08
    • 2020-09-11
    • 1970-01-01
    • 1970-01-01
    • 2021-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多