Search code examples
c#wpfimageoxyplot

How can I return the image?


It is created by a coordinate system with the two codesnippets listed below. Unfortunately, the second code snippet saves the image to the desktop. I would like to have the "image" returned. How can I return the image of the coordinate system? ( I have a method which has a return value as a picture )

At the end it should be preview = image;

So that from the coordinate system an "image" and not to the desktop is stored, but I can return it.

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

Solution

  • First of all, i suggest you to use System.Windows.Media.Imaging.BitmapImage rather than System.Drawing.Image since you are in the WPF-World.

    After you changed that, you can easily write

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

    after the PngExporter did its job.

    Unfortunately i cannot test it since i dont have your pointX and pointY - Collections.

    Let me know if that helps