Click here to Skip to main content
15,914,287 members
Please Sign up or sign in to vote.
2.75/5 (4 votes)
See more:
I want to store screenshots picture in database but its not loaded in the database.
when I executing this program this showing the error the A generic error occurred in GDI+. how can i remove this error ,,,, also I want to store the multiple screenshot at respective time period ,,,, can anybody help me .............?


C#
private void Form2_Load(object sender, EventArgs e)
{
    con1.Open();
    Form1 home = new Form1();
    home.MdiParent = this.MdiParent ;
           
    System.Timers.Timer timer1 = new System.Timers.Timer();
    Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.MyComputer)  + @"\\server1\KamalSingh\ScreenCaptures");
    t.Interval  = 500;
    t.Tick += new EventHandler(StartThread);
    t.Start();
}


System.Windows.Forms.Timer t = new  System.Windows.Forms.Timer();
//Thread tt;   
string i;

        
private static Bitmap bmpscreenshot;
private static Graphics gfxscreenshot;

void TakeScreenShot()
{
    using (Bitmap bmpscreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb))
    {
        using (Graphics gfxscreenshot = Graphics.FromImage(bmpscreenshot))
        {                    
            gfxscreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
            bmpscreenshot.Save(Environment.GetFolderPath(Environment.SpecialFolder.MyComputer) + @"\\server1\KamalSingh\ScreenCaptures\" + i + ".jpeg", ImageFormat.Jpeg );
         }

    }
    //tt.Abort();
}

protected  void StartThread(object sender, EventArgs e)
{
    Thread th  = new Thread(new ThreadStart(TakeScreenShot));
    th.SetApartmentState(ApartmentState.STA );
    th.Start();
    Thread.Sleep(100);
    th.Join();
}

MemoryStream ms;
FileStream st;        

private int  SaveToDB (string st ,string  brt,string  brot, string  spt)
{
    con1.Open();
    SqlCommand cmd = new SqlCommand("dattime", con1);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Connection = con1 ;
    cmd.Parameters.Add("@st", SqlDbType.DateTime ).Value = label2_time1.Text   ;
    cmd.Parameters.Add("@brt", SqlDbType.DateTime ).Value = label2_brkintime.Text  ;
    cmd.Parameters.Add("@brot", SqlDbType.DateTime ).Value = label3_brkofftm.Text ;
    cmd.Parameters.Add("@spt", SqlDbType.DateTime ).Value = label2_time2.Text ;
    cmd.ExecuteReader();
            
    con1.Open();
    int  tm = cmd.ExecuteNonQuery();
    con1.Close();
    return tm;
}

private void pictureBox2_Click(object sender, EventArgs e)
{
    SqlCommand cmdd = new SqlCommand("INSERT INTO imagelog Values(@imagepath,@imgimage)",con1);
    //cmdd.CommandType = CommandType.StoredProcedure;
    con1.Open();
    SqlDataReader dr;
    try
    {
        dr = cmdd.ExecuteReader();
        if (dr.Read())
        {
            byte[] picarr = (byte[])dr[@"\\server1\KamalSingh\ScreenCaptures\"];
            ms = new MemoryStream(picarr);
            ms.Seek(0, SeekOrigin.Begin);
            pictureBox2.Image = Image.FromStream(ms);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    finally
    {
        con1.Close();
    }
    con1.Dispose();
}
Posted
Updated 25-Sep-12 18:22pm
v2
Comments
supernorb 26-Sep-12 7:54am    
Could you please point out or highlight the line where the exception occured? I wonder how fast your database grows in size? :)?
Kay Pee Singh 29-Sep-12 13:35pm    
bmpscreenshot.Save(Environment.GetFolderPath(Environment.SpecialFolder.MyComputer) + @"\\server1\KamalSingh\ScreenCaptures\" + i + ".jpeg", ImageFormat.Jpeg );
supernorb 29-Sep-12 14:12pm    
You should change i after each snapshot being taken!
Kay Pee Singh 29-Sep-12 14:40pm    
can u give me skype id i need ur help ??
supernorb 29-Sep-12 14:51pm    
I don't have any skype id, I have given my solution to you. Isn't it helpful? or at least solved your problem?

Hey, I found your problem!
Just because the file i.jpeg was created the first time and you didn't change i (for example, increase the i if it represents a number in your case), I see you declared i as string and it means you may not want to generate next values for i as numbers?
I declared i as integer and after each snapshot being taken, I increase i by 1, and that worked perfectly.

Hope you understand your problem!

PS: If you are not careful again, this problem may still happen in the future. I means you should have someway to avoid duplicated filenames problem.

Save and Load your image
These are 2 functions to convert between Image and byte[], when insert data to your database, you should have your data as byte[] first and when you load your image data, your will get them as byte[], so you have to convert them to images before you can display them in pictureboxes.
C#
public byte[] ImageToBytes(Image img){
   System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf
 = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
   System.IO.MemoryStream s = new System.IO.MemoryStream();
   bf.Serialize(s,img);
   byte[] data = s.GetBuffer();
   s.Close();
   return data;
}
public Image BytesToImage(byte[] data){
  System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf
 = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
  MemoryStream s = new MemoryStream(data);
  Image img = (Image) bf.Deserialize(s);
  s.Close();
  return img;
}

PS: I've not tested the code, but I think it should work. Try yourself.
 
Share this answer
 
v4
use LinqToSql class model
much easier way
 
Share this answer
 
Comments
NeonMika 27-Sep-12 12:33pm    
I think that this should be just a little tool. And he already has the code. I don't think it is neccessary to change the whole application.
By the way: You are right. :)

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