Plotting Pixels
Well, Microsoft didn't make it easy but I finally worked out how to plot pixels. The trick is to use a Bitmap object and plot the pixels using the SetPixel method.
Then assign the Bitmap to a the Image property of a PictureBox. If the PictureBox has the SizeMode property set to StretchImage then the Bitmap will be stretched or reduced to fit. If you also set the PictureBox Dock property to DockStyle.Fill then you have an automatically resized picture that will stretch as needed.
Here is a simple example (created in SharpDevelop):
// project created on 2003-02-01 at 15:58 using System; using System.Drawing; using System.Windows.Forms; namespace MyFormProject { class MainForm : Form { private System.Windows.Forms.PictureBox pictureBox; Bitmap myBitmap = new Bitmap("auspics.jpg"); //Anna K. looking lovely as usual public MainForm() { InitializeComponents(); // set some pixels, display it in the picture box. for (int Xcount = 0; Xcount < myBitmap.Width; Xcount++) { for (int Ycount = 0; Ycount < myBitmap.Height; Ycount++) {if (Xcount>Ycount) myBitmap.SetPixel(Xcount, Ycount, Color.Black); } } pictureBox.Image = myBitmap; } // This method is used in the forms designer. // Change this method on you own risk void InitializeComponents() { // // Set up generated class MainForm // this.SuspendLayout(); this.Name = "MainForm"; this.Text = "This is my form"; // // Set up member pictureBox // pictureBox = new System.Windows.Forms.PictureBox(); pictureBox.Name = "pictureBox"; pictureBox.Dock = System.Windows.Forms.DockStyle.Fill; pictureBox.SizeMode = PictureBoxSizeMode.StretchImage ; // Stretches the image to fit the pictureBox. this.Controls.Add(pictureBox); this.ResumeLayout(false); } [STAThread] public static void Main(string[] args) { Application.Run(new MainForm()); } } }
No comments:
Post a Comment