0

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;
        }
2
  • This is my method bitmapFromUri: Commented Feb 14, 2013 at 17:34
  • Can you post your xaml too? Commented Feb 14, 2013 at 17:39

2 Answers 2

1

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 ) );
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the response but my code is allready changed i will add it as e new answer..
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.