Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# SQL-Server image
HI
i can not write English very well sorry
me in c# for save a picture in data base use of this code
//////////////////////////////////////////
 
try
{
MemoryStream ms = new MemoryStream();
                pictureBox3.Image.Save(ms, pictureBox3.Image.RawFormat);
                byte[] arrImage = ms.GetBuffer();
                ms.Close();
 
                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));
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
                MessageBox.Show("Insert completed seccessfully."); 
}
 
/////////////////////////////////////
 
and if i want to load a picture to pictureBox use of this code:
/
///////////////////////////////////////////////////
try
               {
                   //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 | :confused: Unsure | :~ OMG | :OMG: Cry | :((
please help me
Posted 7-Mar-11 12:55pm
Edited 7-Mar-11 14:44pm
thatraja242.2K
v2
Rate this: bad
good
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);
mss.Close();
fs.Close();
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Here you go, try this man.
 
C# a generic error occurred in GDI+ Solution[^]
  Permalink  
Rate this: bad
good
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);
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 274
1 Shweta N Mishra 216
2 PIEBALDconsult 210
3 Sergey Alexandrovich Kryukov 185
4 BillWoodruff 174
0 OriginalGriff 7,620
1 Sergey Alexandrovich Kryukov 6,937
2 DamithSL 5,586
3 Manas Bhardwaj 4,946
4 Maciej Los 4,495


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 8 Mar 2011
Copyright © CodeProject, 1999-2014
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