Click here to Skip to main content
13,051,404 members (47,902 online)
Rate this:
Please Sign up or sign in to vote.
See more:
i can not write English very well sorry
me in c# for save a picture in data base use of this code

MemoryStream ms = new MemoryStream();
                pictureBox3.Image.Save(ms, pictureBox3.Image.RawFormat);
                byte[] arrImage = ms.GetBuffer();
                string strSQL = "insert into picture(pic,IDcustomer) values (@pic,@IDcustomer)"; ;
                SqlCommand cmd = new SqlCommand(strSQL, conn);
                cmd.Parameters.Add(new SqlParameter("@pic", SqlDbType.Binary)).Value = arrImage;//picture
                cmd.Parameters.AddWithValue("@IDcustomer", Convert.ToInt16(a));
                MessageBox.Show("Insert completed seccessfully."); 


and if i want to load a picture to pictureBox use of this code:
                   //Get image data from gridview column.
                   byte[] imageData = (byte[])image;
                   //Initialize image variable
                   newImage = null;
                   //Read image data into a memory stream
                   using (MemoryStream ms = new MemoryStream(imageData, 0, imageData.Length))
                       ms.Write(imageData, 0, imageData.Length);
                       //Set image variable value using memory stream.
                       newImage = Image.FromStream(ms, true);
                   //set picture
                   pictureBox1.Image = newImage;
             //      System.IO.File.Create("d:\\");

if i want to save a load picture to a drive use this code :
newImage.Save(@"D:\soft1.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

but this code have run time error
error is
A generic error occurred in GDI+

:confused::~ :omg: :((
please help me
Posted 7-Mar-11 11:55am
Updated 7-Mar-11 13:44pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

1. Create a System.IO.MemoryStream object.
2. Create a System.IO.FileStream object
3. Save image into MemoryStream
4. Read bytes[] from the MemoryStream
5. Save the image file with FileStream
And Here is the C# sample code;
using System.Drawing.Imaging;
using System.Drawing;
using System.IO;
public static void SaveJpeg
(string path, Image img, int quality)
EncoderParameter qualityParam
= new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
ImageCodecInfo jpegCodec
= GetEncoderInfo(@"image/jpeg");
EncoderParameters encoderParams
= new EncoderParameters(1);
encoderParams.Param[0] = qualityParam;
System.IO.MemoryStream mss = new System.IO.MemoryStream();
System.IO.FileStream fs
= new System.IO.FileStream(path, System.IO.FileMode.Create
, System.IO.FileAccess.ReadWrite);
img.Save(mss, jpegCodec, encoderParams);
byte[] matriz = mss.ToArray();
fs.Write(matriz, 0, matriz.Length);
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Rate this: bad
Please Sign up or sign in to vote.

Solution 3

i can to use this
thanks of your help

MemoryStream ms=
new MemoryStream((byte[])dataGridView1["pic", e.RowIndex].Value);
Image img = Image.FromStream(ms,true);
pictureBox1.Image = img;
img.Save("D:\\soft1.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 8 Mar 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100