C# WPF图像不';不随源的变化而变化

C# WPF图像不';不随源的变化而变化,c#,C#,您好,我想通过打开en openfiledialog并选择一个新的来更改图像。。这可以通过更改源来实现。。但是它不起作用。。你能帮帮我吗?这是我的形象 private void pdfOpenen() { Microsoft.Win32.OpenFileDialog d = new Microsoft.Win32.OpenFileDialog(); d.FileName = "Document";//begin map Nullable<bool> pad

您好,我想通过打开en openfiledialog并选择一个新的来更改图像。。这可以通过更改源来实现。。但是它不起作用。。你能帮帮我吗?这是我的形象

private void pdfOpenen()
{
    Microsoft.Win32.OpenFileDialog d = new Microsoft.Win32.OpenFileDialog();
    d.FileName = "Document";//begin map 
    Nullable<bool> pad = d.ShowDialog();
    //Controleren of er een bestand geselecteerd werd
    if (pad == true)
    {
        PaginaHolder.Source = BitmapFromUri(new Uri(pad, UriKind.Relative));
    }
}
public static ImageSource BitmapFromUri(Uri source)
        {
            var bitmap = new BitmapImage();
            bitmap.BeginInit();
            bitmap.UriSource = source;
            bitmap.CacheOption = BitmapCacheOption.OnLoad;
            bitmap.EndInit();
            return bitmap;
        }
private void pdfOpenen()
{
Microsoft.Win32.OpenFileDialog d=新的Microsoft.Win32.OpenFileDialog();
d、 FileName=“Document”;//开始映射
可为空的pad=d.ShowDialog();
//即使是最佳和最佳选择的控制
如果(pad==true)
{
PaginaHolder.Source=BitmapFromUri(新Uri(pad,UriKind.Relative));
}
}
公共静态ImageSource BitmapFromUri(Uri源)
{
var bitmap=新的位图();
bitmap.BeginInit();
bitmap.UriSource=源;
bitmap.CacheOption=BitmapCacheOption.OnLoad;
EndInit();
返回位图;
}

一些应该解决的问题:

PaginaHolder.Source = BitmapFromUri(new Uri(pad, UriKind.Relative));
具体而言:

new Uri(pad, UriKind.Relative)
没有Uri构造函数将nullable bool作为参数。使用:

PaginaHolder.Source = new BitmapImage( new Uri( d.FileName ) );
下面是一个完整的工作示例:

var d = new OpenFileDialog();
d.Title = "Select a picture";
d.Filter = "All supported graphics|*.jpg;*.jpeg;*.png|JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|Portable Network Graphic (*.png)|*.png";
if( d.ShowDialog() == true )
{
    PaginaHolder.Source = new BitmapImage( new Uri( d.FileName ) );
}
您还可以使用
BitmapFromUri
方法:

PaginaHolder.Source = BitmapFromUri( new Uri( d.FileName ) );

我使用一个工作正常的转换器将pdf文档转换为单独的图像。。当我打开一个新的PDF文档时,我的convert会删除前一个图像,然后将新图像添加到地图中。。这很好,但我的应用程序一直显示我的前一个图像,当前一个图像已完全就绪并脱离我的调试映射时,这怎么办。。?这是我的代码:

Microsoft.Win32.OpenFileDialog d = new Microsoft.Win32.OpenFileDialog();
d.FileName = "Document";//begin map 
d.DefaultExt = ".pdf";
d.Filter = "PDF Files(*.pdf)|*.pdf";
Nullable<bool> pad = d.ShowDialog();
pdfPad = d.FileName;
File.Delete(AppDomain.CurrentDomain.BaseDirectory+"1.jpg");
pdfconverter.convertPDF(1, pdfPad);
pdfAantalPaginas = pdfconverter.getAantalPaginas(pdfPad);
Uri test = new Uri(AppDomain.CurrentDomain.BaseDirectory + "1.jpg");
PaginaHolder.Source = BitmapFromUri(test);
PaginaHolder.Source.Freeze();
Microsoft.Win32.OpenFileDialog d=new Microsoft.Win32.OpenFileDialog();
d、 FileName=“文件”//开始地图
d、 DefaultExt=“.pdf”;
d、 Filter=“PDF文件(*.PDF)|*.PDF”;
可为空的pad=d.ShowDialog();
pdfPad=d.FileName;
Delete(AppDomain.CurrentDomain.BaseDirectory+“1.jpg”);
converter.convertPDF(1,pdfPad);
pdfAantalPaginas=pdfconverter.getAantalPaginas(pdfPad);
Uri测试=新Uri(AppDomain.CurrentDomain.BaseDirectory+“1.jpg”);
PaginaHolder.Source=BitmapFromUri(测试);
PaginaHolder.Source.Freeze();

这是我的方法bitmapFromUri:你也可以发布你的xaml吗?谢谢你的回复,但我的代码已经全部更改,我会将其添加为新答案。。