Getting Bitmap to save to file
- Hi, I have a program that generates a Madelbrot Set in a picturebox and I want to save it to a file but when I call a picturebox1.save(...path) function all I get is a blank image, could anyone help me sort this out
The code is in the button 3 click event, but I thought I would post all of my code in case you need it.using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Design; using System.Drawing.Drawing2D; namespace Assignment { /// <summary> /// Summary description for Form1. /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.PictureBox pictureBox1; private Button button1; private Button button2; private Button button3; /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; private Label label1; Rectangle rect; public Form1() { InitializeComponent(); Image bp = new Bitmap(this.pictureBox1.Height, this.pictureBox1.Width); pictureBox1.Image = bp; Graphics g1 = Graphics.FromImage(bp); this.DoubleBuffered = true; } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose(bool disposing) { if (disposing) { if (components != null) { components.Dispose(); } } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1)); this.pictureBox1 = new System.Windows.Forms.PictureBox(); this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.button3 = new System.Windows.Forms.Button(); this.label1 = new System.Windows.Forms.Label(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); this.SuspendLayout(); // // pictureBox1 // this.pictureBox1.BackColor = System.Drawing.SystemColors.Control; this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.pictureBox1.Cursor = System.Windows.Forms.Cursors.Cross; this.pictureBox1.Location = new System.Drawing.Point(57, 77); this.pictureBox1.Name = "pictureBox1"; this.pictureBox1.Size = new System.Drawing.Size(471, 305); this.pictureBox1.TabIndex = 0; this.pictureBox1.TabStop = false; this.pictureBox1.WaitOnLoad = true; this.pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove); this.pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown); this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint); this.pictureBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseUp); // // button1 // this.button1.Location = new System.Drawing.Point(587, 77); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(72, 52); this.button1.TabIndex = 1; this.button1.Text = "Create Fractal"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // button2 // this.button2.Location = new System.Drawing.Point(587, 135); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(72, 52); this.button2.TabIndex = 2; this.button2.Text = "Clear Fractal"; this.button2.UseVisualStyleBackColor = true; this.button2.Click += new System.EventHandler(this.button2_Click_1); // // button3 // this.button3.Location = new System.Drawing.Point(587, 193); this.button3.Name = "button3"; this.button3.Size = new System.Drawing.Size(75, 52); this.button3.TabIndex = 3; this.button3.Text = "Save Fractal"; this.button3.UseVisualStyleBackColor = true; this.button3.Click += new System.EventHandler(this.button3_Click); // // label1 // this.label1.AutoSize = true; this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 30F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.label1.Location = new System.Drawing.Point(49, 9); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(533, 46); this.label1.TabIndex = 4; this.label1.Text = "Mandelbrot Set - Chris Byrne"; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.BackColor = System.Drawing.SystemColors.Control; this.ClientSize = new System.Drawing.Size(780, 394); this.Controls.Add(this.label1); this.Controls.Add(this.button3); this.Controls.Add(this.button2); this.Controls.Add(this.button1); this.Controls.Add(this.pictureBox1); this.DoubleBuffered = true; this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.MaximizeBox = false; this.Name = "Form1"; this.Text = "Mandelbrot Set"; ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); } #endregion /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } public struct HSBColor { float h; float s; float b; int a; public HSBColor(float h, float s, float b) { this.a = 0xff; this.h = Math.Min(Math.Max(h, 0), 255); this.s = Math.Min(Math.Max(s, 0), 255); this.b = Math.Min(Math.Max(b, 0), 255); } public HSBColor(int a, float h, float s, float b) { this.a = a; this.h = Math.Min(Math.Max(h, 0), 255); this.s = Math.Min(Math.Max(s, 0), 255); this.b = Math.Min(Math.Max(b, 0), 255); } public float H { get { return h; } } public float S { get { return s; } } public float B { get { return b; } } public int A { get { return a; } } public Color Color { get { return FromHSB(this); } } public static Color FromHSB(HSBColor hsbColor) { float r = hsbColor.b; float g = hsbColor.b; float b = hsbColor.b; if (hsbColor.s != 0) { float max = hsbColor.b; float dif = hsbColor.b * hsbColor.s / 255f; float min = hsbColor.b - dif; float h = hsbColor.h * 360f / 255f; if (h < 60f) { r = max; g = h * dif / 60f + min; b = min; } else if (h < 120f) { r = -(h - 120f) * dif / 60f + min; g = max; b = min; } else if (h < 180f) { r = min; g = max; b = (h - 120f) * dif / 60f + min; } else if (h < 240f) { r = min; g = -(h - 240f) * dif / 60f + min; b = max; } else if (h < 300f) { r = (h - 240f) * dif / 60f + min; g = min; b = max; } else if (h <= 360f) { r = max; g = min; b = -(h - 360f) * dif / 60 + min; } else { r = 0; g = 0; b = 0; } } return Color.FromArgb ( hsbColor.a, (int)Math.Round(Math.Min(Math.Max(r, 0), 255)), (int)Math.Round(Math.Min(Math.Max(g, 0), 255)), (int)Math.Round(Math.Min(Math.Max(b, 0), 255)) ); } } private const int MAX = 256; // max iterations private const double SX = -2.025; // start value real private const double SY = -1.125; // start value imaginary private const double EX = 0.6; // end value real private const double EY = 1.125; // end value imaginary private static int x1, y1, xs, ys, xe, ye; private static double xstart, ystart, xende, yende, xzoom, yzoom; private static bool action, finished; private static float xy; Graphics g1; public void init() // all instances will be prepared { g1 = pictureBox1.CreateGraphics(); finished = false; x1 = this.pictureBox1.Width; y1 = this.pictureBox1.Height; xy = (float)x1 / (float)y1; finished = true; mandelbrot(); start(); } public void destroy() // delete all instances { if (finished) { g1 = null; GC.Collect(); // garbage collection } } public void start() { action = false; initvalues(); xzoom = (xende - xstart) / (double)x1; yzoom = (yende - ystart) / (double)y1; mandelbrot(); } public void stop() { } public void paint(Graphics g) { //update(g); } //Do Mandelbrot next private void mandelbrot() // calculate all points { int x, y; float h, b, alt = 0.0f; action = false; for (x = 0; x < x1; x += 2) for (y = 0; y < y1; y++) { h = pointcolour(xstart + xzoom * (double)x, ystart + yzoom * (double)y); // color value if (h != alt) { alt = h * 255; b = 1.0f - h * h; // brightnes Color color = HSBColor.FromHSB(new HSBColor(h * 255, 0.8f * 255, b * 255)); // VERY IMPORTANT Pen pen = new Pen(color); g1.DrawLine(pen, x, y, x + 1, y); } } action = true; } private float pointcolour(double xwert, double ywert) // color value from 0.0 to 1.0 by iterations { double r = 0.0, i = 0.0, m = 0.0; int j = 0; while ((j < MAX) && (m < 4.0)) { j++; m = r * r - i * i; i = 2.0 * r * i + ywert; r = m + xwert; } return (float)j / (float)MAX; } private void initvalues() // reset start values { xstart = SX; ystart = SY; xende = EX; yende = EY; if ((float)((xende - xstart) / (yende - ystart)) != xy) xstart = xende - (yende - ystart) * (double)xy; } private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { rect = new Rectangle(e.X, e.Y, 0, 0); this.Invalidate(); if (action) { xs = e.X; ys = e.Y; } } private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) { if (e.Button == MouseButtons.Left) { // Draws the rectangle as the mouse moves rect = new Rectangle(rect.Left, rect.Top, e.X - rect.Left, e.Y - rect.Top); } this.Invalidate(); if (action) { xe = e.X; ye = e.Y; } } private void pictureBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { int z, w; this.pictureBox1.Refresh(); if (action) { xe = e.X; ye = e.Y; if (xs > xe) { z = xs; xs = xe; xe = z; } if (ys > ye) { z = ys; ys = ye; ye = z; } w = (xe - xs); z = (ye - ys); if ((w < 2) && (z < 2)) initvalues(); else { if (((float)w > (float)z * xy)) ye = (int)((float)ys + (float)w / xy); else xe = (int)((float)xs + (float)z * xy); xende = xstart + xzoom * (double)xe; yende = ystart + yzoom * (double)ye; xstart += xzoom * (double)xs; ystart += yzoom * (double)ys; } xzoom = (xende - xstart) / (double)x1; yzoom = (yende - ystart) / (double)y1; mandelbrot(); } } private void button1_Click(object sender, EventArgs e) { init(); start(); button1.Enabled = false; button1.Text = "Fractal Created"; } private void button2_Click(object sender, EventArgs e) { } private void button2_Click_1(object sender, EventArgs e) { DialogResult reply = MessageBox.Show("Do you want to clear the fractal?", "Clear Fractal?", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (reply == DialogResult.Yes) { this.pictureBox1.Invalidate(); this.pictureBox1.Refresh(); button1.Enabled = true; } else { } } private void button3_Click(object sender, EventArgs e) { pictureBox1.Image.Save("C:\\myfractal.bmp", ImageFormatConverter.Bmp); Byte[] b = File.ReadAllBytes("myfractal.bmp"); } private void pictureBox1_Paint(object sender, PaintEventArgs e) { //using (Pen pen = new Pen(Color.Black, 2)) { // e.Graphics.DrawRectangle(pen, rect); } } } }
Answers
To be honest, Im not 100% sure, I just know that without it, it wont draw the fractal.
Is there any way I can save the image with the code I have, or do I need to make changes?
Sorry for all these questions..
You can save the image which is empty, but if you want to save a copy of what you drew to the screen, i'd suggest you draw to the image rather than to the screen.- Marked As Answer byHarry ZhuMSFT, ModeratorFriday, November 13, 2009 1:48 AM
All Replies
G1 can draw to different surfaces depending upon its value. Are you drawing to the screen or to the image?
- Im drawing to a bitmap which is being displayed in the picturebox, I just want what is in the picturebox to be saved as an image file
Im drawing to a bitmap which is being displayed in the picturebox, I just want what is in the picturebox to be saved as an image file
How do you know? What is the purpose of this line:
g1 = pictureBox1.CreateGraphics();
- To be honest, Im not 100% sure, I just know that without it, it wont draw the fractal.
Is there any way I can save the image with the code I have, or do I need to make changes?
Sorry for all these questions.. To be honest, Im not 100% sure, I just know that without it, it wont draw the fractal.
Is there any way I can save the image with the code I have, or do I need to make changes?
Sorry for all these questions..
You can save the image which is empty, but if you want to save a copy of what you drew to the screen, i'd suggest you draw to the image rather than to the screen.- Marked As Answer byHarry ZhuMSFT, ModeratorFriday, November 13, 2009 1:48 AM


