i would like to store one image to databse. while inserting it shows an exception like
"Object reference not set to an instance of an object" in
"pictureBox2.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)" this line
here is my code to insert
MemoryStream ms = new MemoryStream();
pictureBox2.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] photo_aray = new byte[ms.Length];
ms.Position = 0;
ms.Read(photo_aray, 0, photo_aray.Length);
please help to find a solution
thanks
Munesh SharmaPosted Feb 12, 2015, 7:05 AM
You probably shouldn't draw directly on the PictureBox.
You need to use a Bitmap instead. Try putting the bitmap in the PictureBox.Image and then call Save().
http://bytes.com/topic/c-sharp/answers/266074-how-save-picturebox-image-into-jpg-filehttp://dotnet-munesh.blogspot.in/2014/05/how-to-save-image-in-database-in-window.html
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.ImageLocation=@"C:\Documents and Settings\tr1g3800\Desktop\WALKING\30P\100000.jpg" ;
}
private void button2_Click(object sender, EventArgs e)
{
pictureBox1.Image.Save(@"C:\Documents and Settings\tr1g3800\Desktop\WALKING\30P\100000test.jpg",ImageFormat.Jpeg);
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
Bitmap bmp = new Bitmap(pictureBox1.Image);
Graphics gr = Graphics.FromImage(bmp);
Pen p = new Pen(Color.Red);
p.Width = 5.0f;
gr.DrawRectangle(p, 1, 2, 30, 40);
pictureBox1.Image = bmp;
}