WPF图像路径蓝调

时间:2013-06-21 21:28:22

标签: wpf vb.net

我在资源目录中有许多图像,并像这样访问它们......

Public Function ReturnToolBarImage(ByVal ImageName As String) As Image
    Dim UpdateImage As New Image
    With UpdateImage
    End With
    Dim UpdateBitmap As New BitmapImage
    With UpdateBitmap
        .BeginInit()
        .UriSource = New Uri("pack://application:,,,/HOA_Manager_Client_04;component/Resources/" & ImageName, UriKind.Absolute)
        .EndInit()
        .DecodePixelHeight = 32
    End With
    UpdateImage.Source = UpdateBitmap
    Return UpdateImage
End Function

我有控制权坚持使用图像路径而不是图像 - 我可以像这样访问它

.ImageSource = "//application:,,,/HOA_Manager_Client_04;component/Resources/Customers.png"

但是图像是,或者当然,太大了.. Soooooo我添加了一个函数来将调整大小的图像保存到Temp文件夹(即工作)并将路径发回 - 但由于某种原因,它不会工作(没有错误,没有图像)。有什么想法吗?

Public Function ReturnToolBarImageAsString(ByVal ImageName As String) As String
    Dim UpdateImage As New Image
    With UpdateImage
    End With
    Dim UpdateBitmap As New BitmapImage
    With UpdateBitmap
        .BeginInit()
        .UriSource = New Uri("pack://application:,,,/HOA_Manager_Client_04;component/Resources/" & ImageName, UriKind.Absolute)
        .EndInit()
        .DecodePixelHeight = 32
    End With
    Using FS As New IO.FileStream("../../Pages/Temp/" & ImageName, IO.FileMode.Create)
        Dim vEncoder As New PngBitmapEncoder
        vEncoder.Frames.Add(BitmapFrame.Create(UpdateBitmap))
        vEncoder.Save(FS)
    End Using
    'Return "../Temp/" & ImageName
    Return "//application:,,,/HOA_Manager_Client_04;component/Pages/Temp/" & ImageName
End Function

1 个答案:

答案 0 :(得分:0)

通过在Bin目录中运行时创建目录似乎已经解决了问题

Public Function ReturnToolBarImageAsString(ByVal ImageName As String) As String
    Dim UpdateImage As New Image
    With UpdateImage
    End With
    Dim UpdateBitmap As New BitmapImage
    With UpdateBitmap
        .BeginInit()
        .DecodePixelHeight = 32
        .UriSource = New Uri("pack://application:,,,/HOA_Manager_Client_04;component/Resources/" & ImageName, UriKind.Absolute)
        .EndInit()
    End With

    Dim vPath As String = My.Application.Info.DirectoryPath
    If Not IO.Directory.Exists(vPath & "\Temp_Images") Then
        IO.Directory.CreateDirectory(vPath & "\Temp_Images")
    End If
    Using FS As New IO.FileStream(vPath & "\Temp_Images\" & ImageName, IO.FileMode.Create)
        Dim vEncoder As New PngBitmapEncoder
        vEncoder.Frames.Add(BitmapFrame.Create(UpdateBitmap))
        vEncoder.Save(FS)
    End Using
    Return vPath & "\Temp_Images\" & ImageName
End Function