WPF图像没有';t随来源变化而变化

本文关键字:变化 图像 WPF | 更新日期: 2023-09-27 17:59:04

你好,我想打开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;
        }

WPF图像没有';t随来源变化而变化

应该解决的一些问题:

PaginaHolder.Source = BitmapFromUri(new Uri(pad, UriKind.Relative));

具体而言:

new Uri(pad, UriKind.Relative)

没有将可为null的bool作为参数的Uri构造函数。用途:

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文档时,我的转换会删除以前的第一个图像,然后将新图像添加到地图中。。这很好,但我的应用程序一直显示我以前的图像,当以前的图像都准备好从我的调试映射中消失时,这怎么可能呢。。?这是我的代码:

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();