简体   繁体   English

WPF图像不会随着源更改而更改

[英]WPF Image doesn't change with source change

Hello i want to change an image by opening en openfiledialog and selecting a new one.. This By changing the source.. But it doens't work.. Could you please help me? 您好,我想通过打开en openfiledialog并选择一个新映像来更改映像。.通过更改源。.但是它不起作用..能请我帮忙吗? Paginaholder is my image.. Paginaholder是我的形象。

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;
        }

Some things that should be addressed: 应该解决的一些问题:

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

Specifically: 特别:

new Uri(pad, UriKind.Relative)

There is no Uri constructor that takes nullable bool as a parameter. 没有Uri构造函数将可空布尔值作为参数。 Use: 采用:

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

And here is a full working example: 这是一个完整的工作示例:

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

You can also use your BitmapFromUri method: 您还可以使用BitmapFromUri方法:

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

I convert a pdf Document to seperate images with a converter that works fine.. When i open a new PDF Document my convert deletes the previous first image and then adds a the new one to the map.. This works fine but my application keeps showing my previous image, how does this can when the previous image is allready gone out of my debug map..? 我使用工作正常的转换器将pdf文档转换为单独的图像。当我打开新的PDF文档时,convert会删除先前的第一张图像,然后将新图像添加到地图中。我之前的映像,当先前的映像已准备好从调试映射中消失时,该怎么办? This is my code: 这是我的代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM