如何退回图片?

时间:2017-03-08 09:05:37

标签: c# wpf image oxyplot

它由具有下面列出的两个代码片段的坐标系创建。不幸的是,第二个代码段将图像保存到桌面。我想拥有"图像"回。如何返回坐标系的图像? (我有一个返回值为图片的方法)

最后应该是preview = image;

从坐标系中得到一个"图像"而不是存储在桌面上,但我可以将其退回。

var stream = new MemoryStream(); 
var pngExporter = new PngExporter { Width = 600, Height = 400, Background = OxyColors.White };
pngExporter.Export(plotModel, stream);

preview = stream; //Does not work unfortunately
var pngExporter = new PngExporter { Width = 350, Height = 350, Background = OxyColors.White };
pngExporter.ExportToFile(plotModel, @"C:\Users\user\Desktop\test.png");
public bool createPreview(out string errorMessage, out System.Drawing.Image preview, int pWidth, int pHeight, int pMargin)
{
  errorMessage = null;
  preview = null;
  bool folded = false;
  try
  { 

    PlotModel plotModel = new PlotModel { Title = "Vorschaukomponente" };

    plotModel.Axes.Add(new OxyPlot.Axes.LinearAxis { Position = OxyPlot.Axes.AxisPosition.Bottom,  MinimumPadding = 0.1, MaximumPadding = 0.1 });
    plotModel.Axes.Add(new OxyPlot.Axes.LinearAxis { Position = OxyPlot.Axes.AxisPosition.Left, MinimumPadding = 0.1, MaximumPadding = 0.1 });
    var series1 = new OxyPlot.Series.LineSeries
    {
      LineStyle = LineStyle.None,
      MarkerType = MarkerType.Circle,
      MarkerSize = 2,
      MarkerFill = OxyColors.Transparent,
      MarkerStroke = OxyColors.Black,
      MarkerStrokeThickness = 1
    };

    if (pointX.Count == pointY.Count)
    {
      for (int i = 0; i < pointX.Count; i++)
      {
        for (int g = i; g < pointY.Count; g++)
        {
          series1.Points.Add(new DataPoint(pointX[i], pointY[g]));
          Console.WriteLine(i+1 + " | "+ pointX[i].ToString() + "/" + pointY[g]);
          break;
        }
      }
      series1.Smooth = true;
      plotModel.Series.Add(series1);
      try
      {
        var stream = new MemoryStream();
        var pngExporter = new PngExporter { Width = 600, Height = 400, Background = OxyColors.White };
        pngExporter.Export(plotModel, stream);

        preview = stream;
        // var pngExporter = new PngExporter { Width = 350, Height = 350, Background = OxyColors.White };
        // pngExporter.ExportToFile(plotModel, @"C:\Users\user\Desktop\test.png");
        folded = true;
      }
      catch (Exception exc)
      {
        System.Diagnostics.Debug.WriteLine(exc.Message);
        errorMessage = "Es konnt kein Bild erstellt werden.";
        folded = false;
      }
    }
    else
    {
      errorMessage = "Es ist nicht die gleiche Anzahl von xen und yen vorhanden.";
      folded = false;
    }
  }
  catch (Exception)
  {
    errorMessage= "Es trat ein unerwarteter Fehler auf";
    folded = false;
  }
  return folded;
}

2 个答案:

答案 0 :(得分:3)

首先,我建议你使用.index而不是System.Windows.Media.Imaging.BitmapImage,因为你在WPF世界。

更改后,您可以轻松编写

System.Drawing.Image
preview.BeginInit(); preview.StreamSource = stream; preview.EndInit(); 完成工作后

很遗憾我无法测试,因为我没有PngExporterpointX - 收藏品。

如果有帮助,请告诉我

答案 1 :(得分:0)

看起来你想要Image.FromStream(stream)