将位图添加到资源

时间:2013-10-15 15:18:57

标签: arrays vb.net bitmap resources

我正在尝试将压缩位图添加为另一个可执行文件的资源,但却遇到了错误。错误是:

Value of type 'System.Drawing.Bitmap' cannot be converted to '1-dimensional array of System.Drawing.Bitmap'

这是我的伪代码:

模块1:

    Imports System.Runtime.InteropServices
Module ResourceWriter
    Private Function ToPtr(ByVal data As Object) As IntPtr
        Dim h As GCHandle = GCHandle.Alloc(data, GCHandleType.Pinned)
        Dim ptr As IntPtr
        Try
            ptr = h.AddrOfPinnedObject()
        Finally
            h.Free()
        End Try
        Return ptr

    End Function

    <DllImport("kernel32.dll", SetLastError:=True)> _
    Private Function UpdateResource(ByVal hUpdate As IntPtr, ByVal lpType As String, ByVal lpName As String, ByVal wLanguage As UShort, ByVal lpData As IntPtr, ByVal cbData As UInteger) As Boolean
    End Function
    <DllImport("kernel32.dll", SetLastError:=True)> _
    Private Function BeginUpdateResource(ByVal pFileName As String, <MarshalAs(UnmanagedType.Bool)> ByVal bDeleteExistingResources As Boolean) As IntPtr
    End Function
    <DllImport("kernel32.dll", SetLastError:=True)> _
    Private Function EndUpdateResource(ByVal hUpdate As IntPtr, ByVal fDiscard As Boolean) As Boolean
    End Function

    Public Function WriteResource(ByVal filename As String, ByVal bmp As Bitmap()) As Boolean

        Try
            Dim handle As IntPtr = BeginUpdateResource(filename, False)
            Dim file1 As Bitmap() = bmp
            Dim fileptr As IntPtr = ToPtr(file1)
            Dim res As Boolean = UpdateResource(handle, "BitMaps", "0", 0, fileptr, Convert.ToUInt32(file1.Length))
            EndUpdateResource(handle, False)
        Catch ex As Exception
            Return False
        End Try
        Return True

    End Function
End Module

在表格中,按钮下方:

'...here's code to compress the image, commented out for now
Dim bmp1 As Bitmap = Compressed
WriteResource("C:\Users\Admin\Desktop\Testfile.exe", bmp1)

但它不起作用。我应该对模块或按钮下的代码做出哪些更改?在将图像放入资源之前,我发现我应该将 System.Drawing.Bitmap 转换为 1维数组,但是如何?

非常感谢任何帮助:)

修改

我现在尝试了从谷歌和谷歌找到的所有答案MSDN,我无法弄明白。所以,如果有人能够展示如何做到这一点,我真的很感激.. 这是我尝试过的方法之一。

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        '...
        Dim bmp1 As Bitmap = Compressed
        Dim Converted = ConvertToByteArray(bmp1)
        WriteResource("C:\Users\Admin\Desktop\Testfile.exe", Converted)
    End Sub
    Public Shared Function ConvertToByteArray(ByVal value As Bitmap) As Byte()
        Dim bitmapBytes As Byte()
        Using stream As New System.IO.MemoryStream
            value.Save(stream, value.RawFormat)
            bitmapBytes = stream.ToArray
        End Using
        Return bitmapBytes
    End Function

是的,我在Module1将Bitmap()更改为Byte();但它在运行时返回"Value cannot be NULL"

我还尝试将其保存为IO.MemoryStream,然后转换为字节但未成功。

所以,如果任何人可以告诉我如何做到这一点,那真的很棒。

2 个答案:

答案 0 :(得分:1)

您通过在类型名称后面添加()来将参数声明为Bitmap数组:

Public Function WriteResource(ByVal filename As String, ByVal bmp As Bitmap()) As Boolean

如果您不希望它成为数组,请删除():

Public Function WriteResource(ByVal filename As String, ByVal bmp As Bitmap) As Boolean

答案 1 :(得分:0)

你的第一个问题在Ryan的回答中得到了很好的解释(Dim file1 As Bitmap() = bmp也是错误的);第二是你正在掩盖一个不同的问题。

如果您引用UpdateResource on MSDN,您会看到cbdata是要写入的字节数,即位图的字节数。您的代码传递了数组的大小。此外,lpData应该是指向数据的长指针,也是“Note that this is the raw binary data to be stored”。你不能像你想要的那样传递一个位图。

位图类的save方法可以保存到一个内存流,从中可以获取字节AND BYTE COUNT并将其提供给UpdateResource。