I draw using this code which I find it easy when I save just give ayellow background bmp image what's the problem if any one have an idea how to save please tell me

using System.Drawing.Drawing2D;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using WindowsApplication1;

    /// <summary>
    /// Summary description for DrawingShapes.
    /// </summary>
public class DrawingShapes : System.Windows.Forms.Form
{
    internal System.Windows.Forms.ContextMenu mnuLabel;
    internal System.Windows.Forms.MenuItem mnuColorChange;
    internal System.Windows.Forms.ContextMenu mnuForm;
    internal System.Windows.Forms.MenuItem mnuRectangle;
    internal System.Windows.Forms.MenuItem mnuEllipse;
    internal System.Windows.Forms.MenuItem mnuTriangle;
    private System.Windows.Forms.MenuItem mnuRemoveShape;
    private MenuStrip menuStrip1;
    private ToolStripMenuItem fileToolStripMenuItem;
    private ToolStripMenuItem saveToolStripMenuItem;
    private ToolStripMenuItem exitToolStripMenuItem;
    private Bitmap DrawArea;  // make a persistent drawing area

    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;

    public DrawingShapes()
    {
        //
        // Required for Windows Form Designer support
        //
        InitializeComponent();

        //
        // TODO: Add any constructor code after InitializeComponent call
        //
    }

    /// <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()
    {
        this.mnuLabel = new System.Windows.Forms.ContextMenu();
        this.mnuColorChange = new System.Windows.Forms.MenuItem();
        this.mnuRemoveShape = new System.Windows.Forms.MenuItem();
        this.mnuForm = new System.Windows.Forms.ContextMenu();
        this.mnuRectangle = new System.Windows.Forms.MenuItem();
        this.mnuEllipse = new System.Windows.Forms.MenuItem();
        this.mnuTriangle = new System.Windows.Forms.MenuItem();
        this.menuStrip1 = new System.Windows.Forms.MenuStrip();
        this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.saveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.menuStrip1.SuspendLayout();
        this.SuspendLayout();
        // 
        // mnuLabel
        // 
        this.mnuLabel.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.mnuColorChange,
            this.mnuRemoveShape});
        // 
        // mnuColorChange
        // 
        this.mnuColorChange.Index = 0;
        this.mnuColorChange.Text = "Change Color";
        this.mnuColorChange.Click += new System.EventHandler(this.mnuColorChange_Click);
        // 
        // mnuRemoveShape
        // 
        this.mnuRemoveShape.Index = 1;
        this.mnuRemoveShape.Text = "Remove Shape";
        this.mnuRemoveShape.Click += new System.EventHandler(this.mnuRemoveShape_Click);
        // 
        // mnuForm
        // 
        this.mnuForm.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.mnuRectangle,
            this.mnuEllipse,
            this.mnuTriangle});
        // 
        // mnuRectangle
        // 
        this.mnuRectangle.Index = 0;
        this.mnuRectangle.Text = "Create New Rectangle";
        this.mnuRectangle.Click += new System.EventHandler(this.mnuNewShape_Click);
        // 
        // mnuEllipse
        // 
        this.mnuEllipse.Index = 1;
        this.mnuEllipse.Text = "Create New Ellipse";
        this.mnuEllipse.Click += new System.EventHandler(this.mnuNewShape_Click);
        // 
        // mnuTriangle
        // 
        this.mnuTriangle.Index = 2;
        this.mnuTriangle.Text = "Create New Triangle";
        this.mnuTriangle.Click += new System.EventHandler(this.mnuNewShape_Click);
        // 
        // menuStrip1
        // 
        this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.fileToolStripMenuItem});
        this.menuStrip1.Location = new System.Drawing.Point(0, 0);
        this.menuStrip1.Name = "menuStrip1";
        this.menuStrip1.Size = new System.Drawing.Size(628, 24);
        this.menuStrip1.TabIndex = 0;
        this.menuStrip1.Text = "menuStrip1";
        // 
        // fileToolStripMenuItem
        // 
        this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.saveToolStripMenuItem,
            this.exitToolStripMenuItem});
        this.fileToolStripMenuItem.Name = "fileToolStripMenuItem";
        this.fileToolStripMenuItem.Size = new System.Drawing.Size(35, 20);
        this.fileToolStripMenuItem.Text = "File";
        // 
        // saveToolStripMenuItem
        // 
        this.saveToolStripMenuItem.Name = "saveToolStripMenuItem";
        this.saveToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
        this.saveToolStripMenuItem.Text = "Save";
        this.saveToolStripMenuItem.Click += new System.EventHandler(this.saveToolStripMenuItem_Click);
        // 
        // exitToolStripMenuItem
        // 
        this.exitToolStripMenuItem.Name = "exitToolStripMenuItem";
        this.exitToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
        this.exitToolStripMenuItem.Text = "Exit";
        // 
        // DrawingShapes
        // 
        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(628, 426);
        this.ContextMenu = this.mnuForm;
        this.Controls.Add(this.menuStrip1);
        this.MainMenuStrip = this.menuStrip1;
        this.Name = "DrawingShapes";
        this.Text = "DrawingShapes";
        this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.DrawingShapes_MouseDown);
        this.Load += new System.EventHandler(this.DrawingShapes_Load);
        this.menuStrip1.ResumeLayout(false);
        this.menuStrip1.PerformLayout();
        this.ResumeLayout(false);
        this.PerformLayout();

    }
    #endregion

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() 
    {
        Application.Run(new Form2());
    }


    // Keep track of when fake drag or resize mode is enabled.
    private bool isDragging = false;
    private bool isResizing = false;

    // Store the location where the user clicked on the control.
    private int clickOffsetX, clickOffsetY;

    private void lbl_MouseDown(object sender,System.Windows.Forms.MouseEventArgs e)
    {
        // Retrieve a reference to the active label.
        Control currentCtrl;
        currentCtrl = (Control)sender;

        if (e.Button == MouseButtons.Right)
        {
            // Show the context menu.
            currentCtrl.ContextMenu.Show(currentCtrl, new Point(e.X, e.Y));
        }
        else if (e.Button == MouseButtons.Left)
        {
            clickOffsetX = e.X;
            clickOffsetY = e.Y;

            if ((e.X + 5) > currentCtrl.Width || (e.Y + 5) > currentCtrl.Height)
            {
                // The mouse pointer is in the bottom right corner,
                // so resizing mode is appropriate.
                isResizing = true;
            }
            else
            {
                // The mouse is somewhere else, so dragging mode is
                // appropriate.
                isDragging = true;
            }
        }
    }

    private void lbl_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        // Retrieve a reference to the active shape.
        Control currentCtrl;
        currentCtrl = (Control)sender;

        if (isDragging)
        {
            // Move the control.
            currentCtrl.Left = e.X + currentCtrl.Left - clickOffsetX;
            currentCtrl.Top = e.Y + currentCtrl.Top - clickOffsetY;
        }
        else if (isResizing)
        {
            // Resize the control, according to the resize mode.
            if (currentCtrl.Cursor == Cursors.SizeNWSE)
            {
                currentCtrl.Width = e.X;
                currentCtrl.Height = e.Y;
            }
            else if (currentCtrl.Cursor == Cursors.SizeNS)
            {
                currentCtrl.Height = e.Y;
            }
            else if (currentCtrl.Cursor == Cursors.SizeWE)
            {
                currentCtrl.Width = e.X;
            }
        }
        else
        {
            // Change the cursor if the mouse pointer is on one of the edges
            // of the control.
            if (((e.X + 5) > currentCtrl.Width) && 
                ((e.Y + 5) > currentCtrl.Height))
            {
                currentCtrl.Cursor = Cursors.SizeNWSE;
            }
            else if ((e.X + 5) > currentCtrl.Width)
            {
                currentCtrl.Cursor = Cursors.SizeWE;
            }
            else if ((e.Y + 5) > currentCtrl.Height)
            {
                currentCtrl.Cursor = Cursors.SizeNS;
            }
            else
            {
                currentCtrl.Cursor = Cursors.Arrow;
            }
        }

    }
    private void lbl_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        isDragging = false;
        isResizing = false;
    }

    private void mnuColorChange_Click(object sender, System.EventArgs e)
    {
        // Show color dialog.
        ColorDialog dlgColor = new ColorDialog();
        dlgColor.ShowDialog();

        // Change label background.
        mnuLabel.SourceControl.BackColor = dlgColor.Color;

    }

    private void DrawingShapes_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            this.ContextMenu.Show(this, new Point(e.X, e.Y));
        }

    }

    private void mnuNewShape_Click(object sender, System.EventArgs e)
    {
        // Create and configure the shape with some defaults.
        Shape newShape = new Shape();
        newShape.Size = new Size(40, 40);
        newShape.ForeColor = Color.Coral;

        // Configure the appropriate shape depending on the menu option selected.
        if (sender == mnuRectangle)
        {
            newShape.Type = Shape.ShapeType.Rectangle;
        }
        else if (sender == mnuEllipse)
        {
            newShape.Type = Shape.ShapeType.Ellipse;
        }
        else if (sender == mnuTriangle)
        {
            newShape.Type = Shape.ShapeType.Triangle;
        }

        // To determine where to place the shape, you need to convert the 
        // current screen-based mouse coordinates into relative form coordinates.
        newShape.Location = this.PointToClient(Control.MousePosition);

        // Attach a context menu to the shape.
        newShape.ContextMenu = mnuLabel;

        // Connect the shape to all its event handlers.
        newShape.MouseDown += new MouseEventHandler(lbl_MouseDown);
        newShape.MouseMove += new MouseEventHandler(lbl_MouseMove);
        newShape.MouseUp += new MouseEventHandler(lbl_MouseUp);

        // Add the shape to the form.
        this.Controls.Add(newShape);
    }

    private void mnuRemoveShape_Click(object sender, System.EventArgs e)
    {
        Shape ctrlShape = (Shape)mnuLabel.SourceControl;
        this.Controls.Remove(ctrlShape);
    }

    private void DrawingShapes_Load(object sender, EventArgs e)
    {
        DrawArea = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
        InitializeDrawArea();
        Graphics xGraph;
        xGraph = Graphics.FromImage(DrawArea);      // clear the drawing area to background color 
        xGraph.Clear(Color.LightYellow);

    }
    private void InitializeDrawArea()
    { 
    }
    private void saveToolStripMenuItem_Click(object sender, EventArgs e)
    {
              ImageFormat format = ImageFormat.Jpeg;      
        SaveFileDialog sfd = new SaveFileDialog(); 
        sfd.Filter = "JPEG Files(*.jpg)|*.jpg"; 
        if (sfd.ShowDialog()  == DialogResult.OK)     
        {        // now save the image in the DrawArea       
                  DrawArea.Save( sfd.FileName, format );      }
    }
}
public class Shape : System.Windows.Forms.UserControl
{
    // The types of shapes supported by this control.
    public enum ShapeType
    {
        Rectangle,
        Ellipse,
        Triangle
    }

    private ShapeType shape = ShapeType.Rectangle;
    private GraphicsPath path = null;

    public ShapeType Type
    {
        get
        {
            return shape;
        }
        set
        {
            shape = value;
            RefreshPath();
            this.Invalidate();
        }
    }

    // Create the corresponding GraphicsPath for the shape, and apply
    // it to the control by setting the Region property.
    private void RefreshPath()
    {
        path = new GraphicsPath();
        switch (shape)
        {
            case ShapeType.Rectangle:
                path.AddRectangle(this.ClientRectangle);
                break;
            case ShapeType.Ellipse:
                path.AddEllipse(this.ClientRectangle);
                break;
            case ShapeType.Triangle:
                Point pt1 = new Point(this.Width / 2, 0);
                Point pt2 = new Point(0, this.Height);
                Point pt3 = new Point(this.Width, this.Height);
                path.AddPolygon(new Point[] {pt1, pt2, pt3});
                break;
        }
        this.Region = new Region(path);
    }

    protected override void OnResize(System.EventArgs e)
    {
        base.OnResize(e);
        RefreshPath();
        this.Invalidate();
    }

    protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
    {
        base.OnPaint(e);
        if (path != null)
        {
            //e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            //e.Graphics.FillPath(new SolidBrush(this.BackColor), path);
            e.Graphics.DrawPath(new Pen(this.ForeColor, 4), path);
        }
    }

}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

The save code looks fine to me. You should debug it and perhaps check the actual image some more before saving it (manually of course).

I debug but when save give image empty with background yellow what can I do

Bmp.Save(Response.OutputStream, ImageFormat.Gif);
The graphic is saved to the OutputStream and therefore it is displayed in the web page when you compile the application and you have a Gif file.

This article has been dead for over six months. Start a new discussion instead.