Click here to Skip to main content
15,915,839 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hi friends,

This is my code....

On Save Button click i want to save the Image...

Now what problem im facing is when i save th image it is saving blank.
When i open the image it is showing blank...
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        
        Pen mypen;
        bool bmousedown = false;
        Point prevpoint;
        Graphics g;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            mypen = new System.Drawing.Pen(System.Drawing.Color.Black);
            g = pictureBox1.CreateGraphics();
            mypen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
            mypen.Color = Color.Black;
            mypen.Width = 1.0f;
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            prevpoint = e.Location;
            bmousedown = true;
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            bmousedown = false;
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (bmousedown)
            {
                Point thispoint = e.Location;
                if (prevpoint.X == 0 && prevpoint.Y == 0)
                {
                    prevpoint = thispoint;
                    return;
                }
                g.DrawLine(mypen, thispoint.X, thispoint.Y, prevpoint.X, prevpoint.Y);
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                prevpoint = thispoint;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            using (SaveFileDialog sfdlg = new SaveFileDialog())
            {
                 sfdlg.Title="Save Dialog";
                sfdlg.Filter="Bitmap Images (*.bmp)|*.bmp|All files(*.*)|*.*";
                if(sfdlg.ShowDialog(this)==DialogResult.OK)
                {
                    using (Bitmap bmp = new Bitmap(pictureBox1.Width,pictureBox1.Height))
                    {
                        pictureBox1.DrawToBitmap(bmp,new Rectangle(0,0,bmp.Width ,bmp.Height));
                        pictureBox1.Image = new Bitmap(pictureBox1.Width, pictureBox1.Height);
                        pictureBox1.Image.Save("c://cc.Jpg");
                        bmp.Save(sfdlg.FileName);
                        MessageBox.Show("Saved Successfully.....");
                        
                    }
                }
            }
            

        }
    }
}
Posted
Updated 20-May-12 18:51pm
v4

Hi there,

I think this is what your looking for - see the second solution:
http://stackoverflow.com/questions/1063505/c-sharp-how-to-save-a-picturebox-control-as-a-jpeg-file-after-its-edited[^]

Hope this helps,
Ed
 
Share this answer
 
hi dear,

Please refer below link

DrawLines over picturebox Image on mouse click[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900