3

そう、I have imported the canvas into a png file.

キャンバスを保存するためのコードは次のとおりです。

private void CommandBinding_Executed(object sender, RoutedEventArgs e)
        {
            Rect rect = new Rect(canvas1.RenderSize);
            RenderTargetBitmap rtb = new RenderTargetBitmap((int)rect.Right,
              (int)rect.Bottom, 96d, 96d, System.Windows.Media.PixelFormats.Default);
            rtb.Render(canvas1);
            //endcode as PNG
            Microsoft.Win32.SaveFileDialog dl1 = new Microsoft.Win32.SaveFileDialog();
            dl1.FileName = "Sample Image";
            dl1.DefaultExt = ".png";
            dl1.Filter = "Image documents (.png)|*.png";
            Nullable<bool> result = dl1.ShowDialog();
            if (result == true)
            {
                string filename = dl1.FileName;
                BitmapEncoder pngEncoder = new PngBitmapEncoder();
                pngEncoder.Frames.Add(BitmapFrame.Create(rtb));

                //save to memory stream
                System.IO.MemoryStream ms = new System.IO.MemoryStream();

                pngEncoder.Save(ms);
                ms.Close();
                System.IO.File.WriteAllBytes(filename, ms.ToArray());
                Console.WriteLine("Done");

            }

        }

I want to import an image (png file) back to canvas in my application,

つまり、png 画像を WPF キャンバスに開きます。

c#そのため、任意の画像ファイルを WPF のキャンバスにインポートできるコードを親切にダンプしてください。

4

2 に答える 2

6

私はこれがあなたが探しているものだと思いますか?

ImageBrush brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri(@"mypictures\savedimage.png", UriKind.Relative));
canvas.Background = brush;
于 2013-06-03T07:50:25.440 に答える
3

クリシュナのおかげで、最善の解決策は次のようになります。

private void Open_Image(object sender, RoutedEventArgs e)
{
    Microsoft.Win32.OpenFileDialog dl1 = new Microsoft.Win32.OpenFileDialog();
    dl1.FileName = "MYFileSave";
    dl1.DefaultExt = ".png";
    dl1.Filter = "Image documents (.png)|*.png";
    Nullable<bool> result = dl1.ShowDialog();

    if (result == true)
    {
        string filename = dl1.FileName;
        ImageBrush brush = new ImageBrush();
        brush.ImageSource = new BitmapImage(new Uri(@filename, UriKind.Relative));
        canvas1.Background = brush;
    }
}
于 2013-06-03T09:24:59.633 に答える