Pixel plot method errors out without error message.
- by sonny5
// The following method blows up (big red x on screen) without generating error info. Any // ideas why?
// MyPlot.PlotPixel(x, y, Color.BlueViolet, Grf);   // runs if commented out
// My goal is to draw a pixel on a form. Is there a way to increase the pixel size also?
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
public class Plot : System.Windows.Forms.Form
  {
private Size _ClientArea; //keeps the pixels info
private double _Xspan;
private double _Yspan;
public Plot()
{
  InitializeComponent();
}
public Size ClientArea { set { _ClientArea = value; } }
private void InitializeComponent()
{
  this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  this.ClientSize = new System.Drawing.Size(400, 300);
  this.Text="World Plot (world_plot.cs)";
  this.Resize += new System.EventHandler(this.Form1_Resize);
  this.Paint += new System.Windows.Forms.PaintEventHandler(this.doLine);
  this.Paint += new System.Windows.Forms.PaintEventHandler(this.TransformPoints);  // new
  this.Paint += new System.Windows.Forms.PaintEventHandler(this.DrawRectangleFloat);
  this.Paint += new System.Windows.Forms.PaintEventHandler(this.DrawWindow_Paint);
}
private void DrawWindow_Paint(object sender, PaintEventArgs e)
  {
    Graphics Grf = e.Graphics;
    pixPlot(Grf);
  }
static void Main() 
{
  Application.Run(new Plot());
}
private void doLine(object sender, System.Windows.Forms.PaintEventArgs e)
{
  // no transforms done yet!!!
  Graphics g = e.Graphics;
  g.FillRectangle(Brushes.White, this.ClientRectangle);
  Pen p = new Pen(Color.Black);
  g.DrawLine(p, 0, 0, 100, 100);  // draw DOWN in y, which is positive since no matrix called
  p.Dispose();
}
public void PlotPixel(double X, double Y, Color C, Graphics G)
{
  Bitmap bm = new Bitmap(1, 1);
  bm.SetPixel(0, 0, C);  
  G.DrawImageUnscaled(bm, TX(X), TY(Y));
}
private int TX(double X) //transform real coordinates to pixels for the X-axis
{
  double w;
  w = _ClientArea.Width / _Xspan * X + _ClientArea.Width / 2;
  return Convert.ToInt32(w);
}
private int TY(double Y) //transform real coordinates to pixels for the Y-axis
{
  double w;
  w = _ClientArea.Height / _Yspan * Y + _ClientArea.Height / 2;
  return Convert.ToInt32(w);
}
private void pixPlot(Graphics Grf)
{
  Plot MyPlot = new Plot();
  double x = 12.0; 
  double y = 10.0; 
  MyPlot.ClientArea = this.ClientSize;
  Console.WriteLine("x = {0}", x);
  Console.WriteLine("y = {0}", y);
  //MyPlot.PlotPixel(x, y, Color.BlueViolet, Grf);   // blows up
}
private void DrawRectangleFloat(object sender, PaintEventArgs e)
{
  // Create pen.
  Pen penBlu = new Pen(Color.Blue, 2);
  // Create location and size of rectangle.
  float x = 0.0F;
  float y = 0.0F;
  float width = 200.0F;
  float height = 200.0F;    // translate DOWN by 200 pixels
  // Draw rectangle to screen.
  e.Graphics.DrawRectangle(penBlu, x, y, width, height);
}
private void TransformPoints(object sender, System.Windows.Forms.PaintEventArgs e)
   {
     // after transforms 
     Graphics g = this.CreateGraphics();
     Pen penGrn = new Pen(Color.Green, 3);
     Matrix myMatrix2 = new Matrix(1, 0, 0, -1, 0, 0);  // flip Y axis with -1
     g.Transform = myMatrix2;
     g.TranslateTransform(0, 200, MatrixOrder.Append); // translate DOWN the same distance as the rectangle...
     // ...so this will put it at lower left corner
     g.DrawLine(penGrn, 0, 0, 100, 90);   // notice that y 90 is going UP
   }
private void Form1_Resize(object sender, System.EventArgs e)
{
  Invalidate();
}
}