none
how to use a relative path with a bmp System.Drawing.Bitmap

    Question

  •  
    does a relative path work for a bitmap?
    I have an image in the wpf project as resource but i can not load it in a BMP

    Dim BMP As New System.Drawing.Bitmap("/Tools;component/Cursors/TrackShapeBig.png")

    i have no proble when i use a full path but a relative one does not work

    any idea
    Monday, October 26, 2009 5:29 PM

Answers

  • it looks like i found something that work



    Dim BMP As New System.Drawing.Bitmap(Application.GetResourceStream(New Uri("/WpfApplication3;component/cursors/PenBlue.png", UriKind.RelativeOrAbsolute)).Stream)



    Now I need to find a lunatic asylum, chose a room, close the door and throw away the key...
    • Marked as answer by marck68 Tuesday, October 27, 2009 9:00 PM
    Tuesday, October 27, 2009 8:59 PM

All replies

  • Hello:

    Dim BMP As New System.Drawing.Bitmap("pack://application:,,/Resources /Cursors/TrackShapeBig.png")

    Good Luck!
    Monday, October 26, 2009 5:46 PM
  • Hi Marck,

    not sure whether I understand you right - do you really want to use a System.Drawing.Bitmap or are you trying to i.e. set an <Image>'s source prop via code?

    If you have something like a <Image x:Name="imgMyImage"/> in your XAML and assuming that your PNG is found at [solution root]/Cursors/TrackShapeBig.png , you could use the following code to place an image from your resources to it:

          Dim bmp As New BitmapImage(New Uri("/Cursors/TrackShapeBig.png", UriKind.Relative))
    imgMyImage.Source = bmp

    Cheers,
    Olaf
    Monday, October 26, 2009 6:42 PM
  • this

    Dim BMP As New System.Drawing.Bitmap("pack://application:,,/Resources /Cursors/TrackShapeBig.png")

    does not work   "The given path's format is not supported.
    Monday, October 26, 2009 10:03 PM
  • Hello,

    In WPF you should use BitmapImage instead of Bitmap.

    You can load an image compiled as resource like this:
     
    DIM bmp As New BitmapImage(new Uri("pack://application:,,/Resources/Cursors/TrackShapeBig.png"));

    Tuesday, October 27, 2009 11:59 AM
  • Hi Mark,

    if you want to access it from XAML, you willhave to provide an valueConverter like this because WPF doesn't use Bitmaps
    <ValueConversion(GetType(System.Drawing.Image), GetType(BitmapImage))> _
    Public Class BitmapImageToImageSourceConverter
        Implements IValueConverter
    
        Public Sub New()
            Console.WriteLine("converter created")
        End Sub
    
        Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
            Dim image As System.Drawing.Image = CType(value, System.Drawing.Image)
            Dim ms As New MemoryStream
            image.Save(ms, ImageFormat.Png)
            Dim bm As New BitmapImage
            bm.BeginInit()
            bm.StreamSource = ms
            bm.EndInit()
            Return bm
        End Function
    
        Public Function ConvertBack(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
            Throw New NotImplementedException
        End Function
    End Class
    Then it is also possible to use the created "ResourceManager Wrapper" named like your RessourceFile (i.e Translation) in your xaml like this:

    <Binding Converter="{StaticResource ImageConverter}" Source="{x:Static trans:Translation.Winter}">

    Another way is to address the bitmap like this: 

    <Image Source="pack://application:,,,/Translation;component/Graphics/Sample.jpg" Name="myImage"></Image>

    But than the build action of the file has to be set to resource

    Tuesday, October 27, 2009 1:06 PM
  • Hi Marck68,

    as HomerThompson pointed out, you probably don't want to use a System.Drawing.Bitmap (i.e. Windows Forms), but rather a BitmapImage (which is why I asked "do you really want to use a System.Drawing.Bitmap?" earlier).

    It would probably help us to help you if you gave some more info on what you're really after.

    Cheers,
    Olaf
    Tuesday, October 27, 2009 4:13 PM
  • Ok this is what i am trying now to do:


     
    This is the error that I am getting


    Dim myUri As New Uri("/WpfApplication3;component/NewFolder1/PenBlue.png", UriKind.RelativeOrAbsolute)





    Dim bitmap As New BitmapImage
    bitmap.BeginInit()
    bitmap.UriSource = myUri
    bitmap.EndInit()
    Dim mse As New MemoryStream
    Dim EE As New BmpBitmapEncoder
    EE.Frames.Add(BitmapFrame.Create(bitmap))
    EE.Save(mse)
    Dim buffer() As Byte = mse.ToArray
    mse.Close()
    mse.Dispose()

    Return buffer


    Could not find a part of the path 'D:\WpfApplication3;component\NewFolder1\PenBlue.png'.
    Tuesday, October 27, 2009 4:13 PM
  • I tried

    DIM bmp As New BitmapImage(new Uri("pack://application:,,/Resources/Cursors/penblue.png"));

    and it says

    Cannot locate resource 'cursors/penblue.png'.


    the image penblue.png is in the folder Cursors inside the project
    Tuesday, October 27, 2009 4:24 PM
  • Hi Marck68,

    try:

          Dim bitmap As New BitmapImage
          bitmap.BeginInit()
          bitmap.UriSource = New Uri("pack://application:,,,/WpfApplication3;component/Resources/Cursors/penblue.png")
          bitmap.EndInit()
          Dim mse As New System.IO.MemoryStream
          Dim EE As New BmpBitmapEncoder
          EE.Frames.Add(BitmapFrame.Create(bitmap))
          EE.Save(mse)
          Dim buffer() As Byte = mse.ToArray
          mse.Close()
          mse.Dispose()
    
          'Save to disk (into the bin-dir when debugging)
          Dim strStartupPath As String = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName)
          Dim fs As New FileStream(Path.Combine(strStartupPath, "myImage.gif"), FileMode.CreateNew)
          fs.Write(buffer, 0, buffer.Length)
          fs.Dispose()
    


    This should work if your solution is called "WpfApplication3" and your image lies in the folder Resources/Cursors, the folder being directly underneath your solution root.

    FWIW - the syntax of the URI is what you can use if this is being called from a different assembly. If you're calling this from within the same assembly, you can also skip the pack-syntax and use what I showed earlier.

    Cheers,
    Olaf
    Tuesday, October 27, 2009 4:46 PM
  • I tried

    DIM bmp As New BitmapImage(new Uri("pack://application:,,/Resources/Cursors/penblue.png"));

    and it says

    Cannot locate resource 'cursors/penblue.png'.


    the image penblue.png is in the folder Cursors inside the project
    Have you compiled the image as resource?

    Rigth click on the image -> Properties -> Build Action = Resource.

    Tuesday, October 27, 2009 4:58 PM

  • Nope it does not work i get the following error when it assign the uri

    Cannot locate resource 'resources/cursors/penblue.png'.


           bitmap.BeginInit()
         
            bitmap.UriSource = New Uri("pack://application:,,,/WpfApplication3;component/Resources/Cursors/penblue.png")
            bitmap.EndInit()



    the uri below  work for for the bitmap

      bitmap.UriSource = New Uri("/WpfApplication3;component/cursors/PenBlue.png", UriKind.RelativeOrAbsolute)

    but not for the BitmapFrame.Create(bitmap)
    Tuesday, October 27, 2009 5:09 PM
  • Hi Marck,

    would you please, besides assuring that your image is compiled as a resource (see HomeroThompson's post), that you're using the correct paths. In your last post you were using /cursors/PenBlue.png in once place and /Resources/Cursors/penblue.png in another.
    I have actually tested the code that I showed in my last posting and everything works as expected provided ...
    • the path information is correct/valid
    • the PNG's BuildAction is set to "Resource"
    • the PNG is a valid PNG-file

    Cheers,
    Olaf
    Tuesday, October 27, 2009 5:49 PM
  • thanks for the help Guys


    the following code crashers  where indicated by the arrow

            Dim bitmap As New BitmapImage
            bitmap.BeginInit()
            bitmap.UriSource = New Uri("/WpfApplication3;component/cursors/PenBlue.png", UriKind.RelativeOrAbsolute)
            bitmap.EndInit()
            Dim mse As New System.IO.MemoryStream
            Dim EE As New BmpBitmapEncoder
            EE.Frames.Add(BitmapFrame.Create(bitmap))           <------------------------------------- Here
            EE.Save(mse)
            Dim buffer() As Byte = mse.ToArray
            mse.Close()
            mse.Dispose()

    with this message:

    Could not find a part of the path 'D:\WpfApplication3;component\cursors\PenBlue.png'.




    This other code crushes in the point indicated by the arrow



     Dim bitmap As New BitmapImage
            bitmap.BeginInit()
           bitmap.UriSource = New Uri("pack://application:,,,/WpfApplication3;component/Cursors/penblue.png")
            bitmap.EndInit()                                                      <-----------------------------------------------Here
            Dim mse As New System.IO.MemoryStream
            Dim EE As New BmpBitmapEncoder
            EE.Frames.Add(BitmapFrame.Create(bitmap))
            EE.Save(mse)
            Dim buffer() As Byte = mse.ToArray
            mse.Close()
            mse.Dispose()

    with the following message

    cannot locate resource 'cursors/penblue.png'.



    If I drag the picture on the code editor i get this actual address

    D:\Trials\WpfApplication3\WpfApplication3\Cursors\PenBlue.png
    The png image is a valid one and it is set as Resource
    Tuesday, October 27, 2009 7:58 PM
  • it looks like i found something that work



    Dim BMP As New System.Drawing.Bitmap(Application.GetResourceStream(New Uri("/WpfApplication3;component/cursors/PenBlue.png", UriKind.RelativeOrAbsolute)).Stream)



    Now I need to find a lunatic asylum, chose a room, close the door and throw away the key...
    • Marked as answer by marck68 Tuesday, October 27, 2009 9:00 PM
    Tuesday, October 27, 2009 8:59 PM