Share via


How to make a dll with PNG or Jpeg images? and a dll with mp3?

Question

Saturday, July 18, 2009 7:41 AM

Hi,

I'm using VB .Net 2008 and I need to make a dll with images and mp3. How to make it?

Lazie

Brazi

All replies (10)

Monday, July 20, 2009 8:43 PM ✅Answered | 1 vote

Imports System.Drawing  : The error on this line was due to reference. When you create windows Form application, it automatically imports the drawing class but in the case of class library you have to manually add the reference to your project. Im sure you know how to add reference, just add the lines below and it will fix the imports error.

System.Drawing.Design.dll
System.Drawing.dll

kaymaf

I hope this helps, if that is what you want, just mark it as answer so that we can move on


Saturday, July 18, 2009 11:57 AM

My Project -> Resources -> Add Resource, if you want your dll to send ect the image you can make a shared funtion for it

Shared Function GetImage() As Byte
Dim ReadImage As New System.IO.BinaryReader(My.Resources.Image000)
Dim Buffer As Byte()
ReadImage.Read(Buffer, 0, My.Resources.Image000.Lenght)
ReadImage.Close()
Return Buffer
End Function

Saturday, July 18, 2009 12:41 PM | 1 vote

To make a files library (Dll) , The steps below

  1. Create a new project, and select Class Library instead of windows Application.
  2. To add a file in the project Right click on the project name in Solution Explorer, select “Add” >> “Existing Item…”
  3. Select all the files ( images and MP3) you added in solution explorer, and change their "Build Action" property to "Embedded Resources"
    4)  To compile, Go to Build menu, select "Build Solution" or "Build yourProjectName"

kaymaf 
I hope this helps, if that is what you want, just mark it as answer so that we can move on


Saturday, July 18, 2009 9:55 PM

Hi Kaymaf,
I made the dll with one bitmap only, with "embedded resources".
So I reference the dll in my vb project.

But now I don't know how to load that bitmap.

Thanks for the reply


Sunday, July 19, 2009 1:28 AM | 2 votes

In your DLL project, create a method to extract or load files from embedded resources, then in your vb project, call the method from the DLL file. Below is sample code from my project. The Dll name is "myImageLib" is also the same as my namespace. I created method to get all the list of files that i embedded, method to load image, icon. Also method to play audio wav from the embedded, method to load and save file from embedded resources. If you have any question about the code, let me know.

Imports System
Imports System.Collections
Imports System.Text
Imports System.Reflection
Imports System.IO
Imports System.Drawing
Namespace myImageLib  'this will be the name of your project

    Public Class myImages   '
       
        Private Shared nameSp As String = ""
        Private Shared nameSpaceLenght As Integer
        Private Shared Function GetNames() As String()
            Dim a As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()
            nameSp = a.GetName().Name.ToString
            nameSpaceLenght = nameSp.Length + 1
            Return a.GetManifestResourceNames()
        End Function

        Public Shared Function GetImageByResourceName(ByVal resourceName As String) As Image
            Dim a As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()
            Dim my_namespace As String = a.GetName().Name.ToString
            Try
                Dim stream As Stream = a.GetManifestResourceStream(my_namespace + "." + resourceName)
                Return DirectCast(Bitmap.FromStream(stream), Bitmap)
            Catch ex As Exception
                Return Nothing
            End Try
        End Function
        Public Shared Function GetIconByResourceName(ByVal resourceName As String) As Icon
            Dim a As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()
            Dim my_namespace As String = a.GetName().Name.ToString
            Try
                Dim stream As Stream = a.GetManifestResourceStream(my_namespace + "." + resourceName)
                Return DirectCast(New Drawing.Icon(stream), Icon)
            Catch ex As Exception
                Return Nothing
            End Try
        End Function
'This will get the list of all files you embedded in the file
        Public Shared Function FileList() As ArrayList
            Dim arrayFile As New ArrayList
            Dim AllFiles() As String = GetNames()
            For Each fname As String In AllFiles
                fname = Trim(fname.Remove(0, nameSpaceLenght))
                arrayFile.Add(fname)
            Next
            Return arrayFile
        End Function
'extract file from resource,work for any file
        Public Shared Function SaveEmbeddedResourcesFile(ByVal fileToGetFromAssembly As String, ByVal LocationTosavefileTo As String)
            Try
                Dim xCount As Integer
                Dim a As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()
                Dim my_namespace As String = a.GetName().Name.ToString
                Dim resFilestream As Stream = a.GetManifestResourceStream(my_namespace + "." + fileToGetFromAssembly)
                Dim writeResources As New FileStream(LocationTosavefileTo, FileMode.OpenOrCreate)
                For xCount = 1 To resFilestream.Length
                    writeResources.WriteByte(resFilestream.ReadByte)
                Next
                writeResources.Close()
                resFilestream.Close()
            Catch ex As Exception
                MsgBox(ex.Message.ToString)
            End Try
        End Function
'Read textfile from resources 
        Public Shared Function readTextFileFromEmbeddedResources(ByVal filename As String) As String
            Dim txtFile As String
            Dim a As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()
            Dim my_namespace As String = a.GetName().Name.ToString
            Dim resFilestream As Stream = a.GetManifestResourceStream(my_namespace + "." + filename)
            If Not (resFilestream Is Nothing) Then
                Dim stream_reader As New StreamReader(resFilestream)
                txtFile = stream_reader.ReadToEnd()
                stream_reader.Close()
                Return txtFile
            End If
        End Function
'below is the function to play audio wav file from embedded resource
 
        Public Declare Function PlaySound Lib "Winmm.dll" (ByVal data() As Byte, ByVal hMod As IntPtr, ByVal dwFlags As Int32) As Boolean
        Public Const SND_ASYNC As Int32 = 1 ' play asynchronously 
        Public Const SND_MEMORY As Int32 = 4 'Play wav in memory
        Public Const SND_LOOP As Int32 = 8 'loop
        Public Shared Sub PlayWavFileFromResource(ByVal filename As String, Optional ByVal loopAudio As Boolean = False)
            Dim a As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()
            Dim my_namespace As String = a.GetName().Name.ToString
            Dim resFilestream As Stream = a.GetManifestResourceStream(my_namespace + "." + filename)
            If (resFilestream Is Nothing) Then
                Exit Sub
            End If
            ' bring stream into a byte array
            Dim ByteStream(resFilestream.Length) As Byte
            resFilestream.Read(ByteStream, 0, CType(resFilestream.Length, Integer))
            ' play the resource
            If loopAudio = True Then 'check if loop audio is true
                PlaySound(ByteStream, IntPtr.Zero, SND_ASYNC Or SND_MEMORY Or SND_LOOP)
            Else
                PlaySound(ByteStream, IntPtr.Zero, SND_ASYNC Or SND_MEMORY)
            End If

        End Sub
      'stop the audio
        Public Shared Sub StopAudio() 
            PlaySound(Nothing, IntPtr.Zero, SND_ASYNC Or SND_MEMORY)
        End Sub
    End Class
End Namespace

" To use above dll file in your vb windows form application, make sure you add reference to your DLL
Imports myImageLib 'this your DLL library
Imports System.IO
Imports System.Reflection

Dim em As myImageLib.myImageLib.myImages ' create instance of the DLL class
Dim arr As New ArrayList 'hold the files list of embedded resources
Dim im As Image
Dim ic As Icon
Private Sub EmbeDLLimage_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        
 arr = em.FileList 'get all the files names
 ic = em.GetIconByResourceName("myicon.ico") 'get icon
 im = em.GetImageByResourceName("myimage.png")  'get image
 em.SaveEmbeddedResourcesFile("myImage.png", Application.StartupPath & "\myImage.png") 'get file and save it 
 TextBox1.Text = em.readTextFileFromEmbeddedResources("myText.txt") 'read text file from resources
       
 End Sub

kaymafI hope this helps, if that is what you want, just mark it as answer so that we can move on


Monday, July 20, 2009 12:33 PM

Is your program working now or you gave up on this thread

kaymaf

I hope this helps, if that is what you want, just mark it as answer so that we can move on


Monday, July 20, 2009 3:50 PM

I will try this code tonight


Monday, July 20, 2009 6:51 PM

Keymaf,  the part from "Imports System ... " to  "    End Class End Namespace",  I put in the DLL project, and I rename "Namespace myImageLib" to "Namespace Img1".  Img1.dll is the name of my DLL.
The rest of the code I put in my vb project.

In the code that I put in the DLL, an error is occuring in this line: Imports System.Drawing

I don't remember the message error now, I'm not in my house, tonight I will post the message error here


Monday, July 20, 2009 6:52 PM

Keymaf,  the part from "Imports System ... " to  "    End Class End Namespace",  I put in the DLL project, and I rename "Namespace myImageLib" to "Namespace Img1".  Img1.dll is the name of my DLL.
The rest of the code I put in my vb project.

In the code that I put in the DLL, an error is occuring in this line: Imports System.Drawing

I don't remember the message error now, I'm not in my house, tonight I will post the message error here


Monday, July 20, 2009 10:22 PM

WORKS Keymaf!!!!  Works

Thanks

But I have one problem with this method... the PNG transparency is not working... I will post another question...