C# 如何返回图像?

C# 如何返回图像?,c#,wpf,image,oxyplot,C#,Wpf,Image,Oxyplot,它由一个坐标系和下面列出的两个代码段创建。不幸的是,第二个代码段将图像保存到桌面。我想返回“图像”。如何返回坐标系的图像?(我有一个返回值为图片的方法) 最后应该是preview=image 因此,从坐标系中存储“图像”,而不是存储到桌面,但我可以返回它 var stream = new MemoryStream(); var pngExporter = new PngExporter { Width = 600, Height = 400, Background = OxyColors.Wh

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

最后应该是
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


public bool createPreview(输出字符串错误消息、输出System.Drawing.Image预览、int pWidth、int pHeight、int pArgin)
{
errorMessage=null;
预览=空;
bool=false;
尝试
{ 
PlotModel PlotModel=新的PlotModel{Title=“Vorschaukomponente”};
plotModel.Axis.Add(新的OxyPlot.Axis.LinearAxis{Position=OxyPlot.Axis.AxisPosition.Bottom,MinimumPadding=0.1,MaximumPadding=0.1});
plotModel.Axis.Add(新的OxyPlot.Axis.LinearAxis{Position=OxyPlot.Axis.AxisPosition.Left,MinimumPadding=0.1,MaximumPadding=0.1});
var series1=新的OxyPlot.Series.LineSeries
{
LineStyle=LineStyle.None,
MarkerType=MarkerType.Circle,
MarkerSize=2,
MarkerFill=OxyColors.透明,
MarkerStroke=OxyColor.黑色,
MarkerStrokeThickness=1
};
if(pointX.Count==pointY.Count)
{
for(int i=0;i
看起来像是您想要的

首先,我建议您使用
System.Windows.Media.Imaging.BitmapImage
而不是
System.Drawing.Image
,因为您身处WPF世界

在你改变了这一点之后,你就可以轻松地写作了

 preview.BeginInit();
 preview.StreamSource = stream;
 preview.EndInit();
PngExporter
完成其工作之后

不幸的是,我无法测试它,因为我没有您的
pointX
pointY
-集合


如果有帮助,请告诉我

无法将“System.Drawing.Image”转换为“System.Uri”<代码>System.Drawing.Image sa;BitmapEncoder编码器=新的PngBitmapEncoder();Encoder.Frames.Add(BitmapFrame.Create(sa));保存(文件流)
现在在主窗口中,出了什么问题?这是一个完全不同的问题。信息太少,无法回答,请提出新问题。不幸的是,我无法测试您的解决方法,但之后我将提出新问题。谢谢你的回复。
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;
}
 preview.BeginInit();
 preview.StreamSource = stream;
 preview.EndInit();