C# 从C:\Program Files(x86)或通过C:\Program Files运行后,WPF应用程序崩溃

C# 从C:\Program Files(x86)或通过C:\Program Files运行后,WPF应用程序崩溃,c#,encryption,wpf-4.0,C#,Encryption,Wpf 4.0,我在WPF中制作了一个应用程序,通过REST API通过发出请求和获得响应来调用数据,在得到响应后,我下载所有图像文件&加密并保存在我的本地驱动器上,将本地加密图像的路径放入我的应用程序中的本地数据库(SQL server Compact 4.0) Image img_home = new Image(); img_home.Width = 1920; img_home.Height = 1080; img_home.Source = DecryptFile.LoadBitmapImages

我在WPF中制作了一个应用程序,通过REST API通过发出请求和获得响应来调用数据,在得到响应后,我下载所有图像文件&加密并保存在我的本地驱动器上,将本地加密图像的路径放入我的应用程序中的本地数据库(SQL server Compact 4.0)

Image img_home = new Image();
img_home.Width = 1920;
img_home.Height = 1080;  
img_home.Source = DecryptFile.LoadBitmapImages(mediaPath); 
grid_main_Home_page.Children.Add(img_home);

//where grid_main_Home_page is a Grid in application

public static BitmapSource LoadBitmapImages(string ImagePath_to_Save)
{
    byte[] ImageBytes;
    BitmapSource bs1;
    // if (File.Exists(ImagePath_to_Save))
    //{
        ImageBytes = File.ReadAllBytes(ImagePath_to_Save);
        using (System.IO.MemoryStream Stream = new System.IO.MemoryStream(ImageBytes))
        {
            Bitmap source = new Bitmap(Stream);
            IntPtr ip = source.GetHbitmap();
            bs1 = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, Int32Rect.Empty,

            System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());

            return bs1;
        }
    // }
}
以上所有进程都在后台线程中运行

Image img_home = new Image();
img_home.Width = 1920;
img_home.Height = 1080;  
img_home.Source = DecryptFile.LoadBitmapImages(mediaPath); 
grid_main_Home_page.Children.Add(img_home);

//where grid_main_Home_page is a Grid in application

public static BitmapSource LoadBitmapImages(string ImagePath_to_Save)
{
    byte[] ImageBytes;
    BitmapSource bs1;
    // if (File.Exists(ImagePath_to_Save))
    //{
        ImageBytes = File.ReadAllBytes(ImagePath_to_Save);
        using (System.IO.MemoryStream Stream = new System.IO.MemoryStream(ImageBytes))
        {
            Bitmap source = new Bitmap(Stream);
            IntPtr ip = source.GetHbitmap();
            bs1 = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, Int32Rect.Empty,

            System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());

            return bs1;
        }
    // }
}
现在,应用程序的表示层总是在本地数据库中查找,如果获得任何图像路径,应用程序将按照以下方式对其解密并使用它在表示层上显示

Image img_home = new Image();
img_home.Width = 1920;
img_home.Height = 1080;  
img_home.Source = DecryptFile.LoadBitmapImages(mediaPath); 
grid_main_Home_page.Children.Add(img_home);

//where grid_main_Home_page is a Grid in application

public static BitmapSource LoadBitmapImages(string ImagePath_to_Save)
{
    byte[] ImageBytes;
    BitmapSource bs1;
    // if (File.Exists(ImagePath_to_Save))
    //{
        ImageBytes = File.ReadAllBytes(ImagePath_to_Save);
        using (System.IO.MemoryStream Stream = new System.IO.MemoryStream(ImageBytes))
        {
            Bitmap source = new Bitmap(Stream);
            IntPtr ip = source.GetHbitmap();
            bs1 = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, Int32Rect.Empty,

            System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());

            return bs1;
        }
    // }
}
当我在我的机器中的任何地方直接通过.exe运行应用程序时,我的应用程序工作正常

Image img_home = new Image();
img_home.Width = 1920;
img_home.Height = 1080;  
img_home.Source = DecryptFile.LoadBitmapImages(mediaPath); 
grid_main_Home_page.Children.Add(img_home);

//where grid_main_Home_page is a Grid in application

public static BitmapSource LoadBitmapImages(string ImagePath_to_Save)
{
    byte[] ImageBytes;
    BitmapSource bs1;
    // if (File.Exists(ImagePath_to_Save))
    //{
        ImageBytes = File.ReadAllBytes(ImagePath_to_Save);
        using (System.IO.MemoryStream Stream = new System.IO.MemoryStream(ImageBytes))
        {
            Bitmap source = new Bitmap(Stream);
            IntPtr ip = source.GetHbitmap();
            bs1 = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, Int32Rect.Empty,

            System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());

            return bs1;
        }
    // }
}
当我通过C:\Program Files(x86)\OR C:\Program Files运行我的应用程序时,它将由于显示以下错误而崩溃--

Image img_home = new Image();
img_home.Width = 1920;
img_home.Height = 1080;  
img_home.Source = DecryptFile.LoadBitmapImages(mediaPath); 
grid_main_Home_page.Children.Add(img_home);

//where grid_main_Home_page is a Grid in application

public static BitmapSource LoadBitmapImages(string ImagePath_to_Save)
{
    byte[] ImageBytes;
    BitmapSource bs1;
    // if (File.Exists(ImagePath_to_Save))
    //{
        ImageBytes = File.ReadAllBytes(ImagePath_to_Save);
        using (System.IO.MemoryStream Stream = new System.IO.MemoryStream(ImageBytes))
        {
            Bitmap source = new Bitmap(Stream);
            IntPtr ip = source.GetHbitmap();
            bs1 = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, Int32Rect.Empty,

            System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());

            return bs1;
        }
    // }
}
当我在目录中查找图像文件时,如果我将该部分代码放入中,请尝试catch block,但仍然会得到相同的错误

Image img_home = new Image();
img_home.Width = 1920;
img_home.Height = 1080;  
img_home.Source = DecryptFile.LoadBitmapImages(mediaPath); 
grid_main_Home_page.Children.Add(img_home);

//where grid_main_Home_page is a Grid in application

public static BitmapSource LoadBitmapImages(string ImagePath_to_Save)
{
    byte[] ImageBytes;
    BitmapSource bs1;
    // if (File.Exists(ImagePath_to_Save))
    //{
        ImageBytes = File.ReadAllBytes(ImagePath_to_Save);
        using (System.IO.MemoryStream Stream = new System.IO.MemoryStream(ImageBytes))
        {
            Bitmap source = new Bitmap(Stream);
            IntPtr ip = source.GetHbitmap();
            bs1 = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, Int32Rect.Empty,

            System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());

            return bs1;
        }
    // }
}
如果我把图像源像

Image img_home = new Image();
img_home.Width = 1920;
img_home.Height = 1080;  
img_home.Source = DecryptFile.LoadBitmapImages(mediaPath); 
grid_main_Home_page.Children.Add(img_home);

//where grid_main_Home_page is a Grid in application

public static BitmapSource LoadBitmapImages(string ImagePath_to_Save)
{
    byte[] ImageBytes;
    BitmapSource bs1;
    // if (File.Exists(ImagePath_to_Save))
    //{
        ImageBytes = File.ReadAllBytes(ImagePath_to_Save);
        using (System.IO.MemoryStream Stream = new System.IO.MemoryStream(ImageBytes))
        {
            Bitmap source = new Bitmap(Stream);
            IntPtr ip = source.GetHbitmap();
            bs1 = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, Int32Rect.Empty,

            System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());

            return bs1;
        }
    // }
}
img_home.Source =new BitmapImage(new Uri(mediaPath,UriKind.Absolute)); 
它工作正常,但我不想要它,因为这里我需要解密文件并将其保存在本地驱动器中

Image img_home = new Image();
img_home.Width = 1920;
img_home.Height = 1080;  
img_home.Source = DecryptFile.LoadBitmapImages(mediaPath); 
grid_main_Home_page.Children.Add(img_home);

//where grid_main_Home_page is a Grid in application

public static BitmapSource LoadBitmapImages(string ImagePath_to_Save)
{
    byte[] ImageBytes;
    BitmapSource bs1;
    // if (File.Exists(ImagePath_to_Save))
    //{
        ImageBytes = File.ReadAllBytes(ImagePath_to_Save);
        using (System.IO.MemoryStream Stream = new System.IO.MemoryStream(ImageBytes))
        {
            Bitmap source = new Bitmap(Stream);
            IntPtr ip = source.GetHbitmap();
            bs1 = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, Int32Rect.Empty,

            System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());

            return bs1;
        }
    // }
}
请帮帮我,我正在努力

Image img_home = new Image();
img_home.Width = 1920;
img_home.Height = 1080;  
img_home.Source = DecryptFile.LoadBitmapImages(mediaPath); 
grid_main_Home_page.Children.Add(img_home);

//where grid_main_Home_page is a Grid in application

public static BitmapSource LoadBitmapImages(string ImagePath_to_Save)
{
    byte[] ImageBytes;
    BitmapSource bs1;
    // if (File.Exists(ImagePath_to_Save))
    //{
        ImageBytes = File.ReadAllBytes(ImagePath_to_Save);
        using (System.IO.MemoryStream Stream = new System.IO.MemoryStream(ImageBytes))
        {
            Bitmap source = new Bitmap(Stream);
            IntPtr ip = source.GetHbitmap();
            bs1 = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, Int32Rect.Empty,

            System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());

            return bs1;
        }
    // }
}
问候

Image img_home = new Image();
img_home.Width = 1920;
img_home.Height = 1080;  
img_home.Source = DecryptFile.LoadBitmapImages(mediaPath); 
grid_main_Home_page.Children.Add(img_home);

//where grid_main_Home_page is a Grid in application

public static BitmapSource LoadBitmapImages(string ImagePath_to_Save)
{
    byte[] ImageBytes;
    BitmapSource bs1;
    // if (File.Exists(ImagePath_to_Save))
    //{
        ImageBytes = File.ReadAllBytes(ImagePath_to_Save);
        using (System.IO.MemoryStream Stream = new System.IO.MemoryStream(ImageBytes))
        {
            Bitmap source = new Bitmap(Stream);
            IntPtr ip = source.GetHbitmap();
            bs1 = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, Int32Rect.Empty,

            System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());

            return bs1;
        }
    // }
}

Anupam mishra

在比Windows XP更新的Windows版本中,您无法写入程序目录。这是一件好事。有一个专门的用户数据文件夹。您可以在其中查询并保存数据:

Image img_home = new Image();
img_home.Width = 1920;
img_home.Height = 1080;  
img_home.Source = DecryptFile.LoadBitmapImages(mediaPath); 
grid_main_Home_page.Children.Add(img_home);

//where grid_main_Home_page is a Grid in application

public static BitmapSource LoadBitmapImages(string ImagePath_to_Save)
{
    byte[] ImageBytes;
    BitmapSource bs1;
    // if (File.Exists(ImagePath_to_Save))
    //{
        ImageBytes = File.ReadAllBytes(ImagePath_to_Save);
        using (System.IO.MemoryStream Stream = new System.IO.MemoryStream(ImageBytes))
        {
            Bitmap source = new Bitmap(Stream);
            IntPtr ip = source.GetHbitmap();
            bs1 = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, Int32Rect.Empty,

            System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());

            return bs1;
        }
    // }
}
// Sample for the Environment.GetFolderPath method 
using System;

class Sample 
{
    public static void Main() 
    {
       var folder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
       Console.WriteLine(folder);
       Console.ReadLine();
    }
}

若我将数据保存在文档目录或其他地方&运行的应用程序仍然会收到相同的错误。实际上,我的后台进程(我指的是后台线程)工作正常&它将数据保存在程序目录中。但是应用程序使用数据时出现问题。捕获异常并显示一个带有应用程序查找路径的messagebox。很可能是错误的。将路径粘贴到资源管理器中,您将得到相同的错误。y我这样做了,&这是正确的路径,i think问题出现在我将内存中的图像解密为BitmapSource并在应用程序中使用的地方。但如果它在其他地方工作正常,那么为什么在应用程序从程序目录运行时会出现问题呢?因此,您需要放入更多的try/catch块或行号,并查看引发异常的位置。感谢chris的编辑邮报。