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

2 回答 2

1

一些应该解决的问题:

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

具体来说:

new Uri(pad, UriKind.Relative)

没有将可为空的 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 ) );
于 2013-02-14T17:41:48.407 回答
0

我使用可以正常工作的转换器将 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();
于 2013-02-15T12:49:22.920 回答