简体   繁体   中英

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? Paginaholder is my image..

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. 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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