我怎样才能 return 图片?

How can I return the image?

它是由坐标系统创建的,下面列出了两个代码片段。不幸的是,第二个代码片段将图像保存到桌面。我想要 "image" returned。我怎样才能return图像的坐标系? (我有一个方法,它有一个 return 值作为图片)

最后应该是preview = image;

所以从坐标系一个"image"而不是桌面存储,但我可以return它。

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

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

首先,我建议您使用 System.Windows.Media.Imaging.BitmapImage 而不是 System.Drawing.Image,因为您在 WPF-World.

改完之后,就可以轻松写

 preview.BeginInit();
 preview.StreamSource = stream;
 preview.EndInit();

PngExporter 完成它的工作之后。

不幸的是我无法测试它,因为我没有你的 pointXpointY - Collections.

如果有帮助请告诉我