Click here to Skip to main content
12,243,721 members (47,457 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#3.0 C# C#4.0
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 .............?


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 25-Sep-12 13:28pm
Edited 25-Sep-12 19: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?
Kay Pee Singh 29-Sep-12 15:40pm
   
yeah that worked i need some more help about other of my project
supernorb 29-Sep-12 16:00pm
   
I don't think I'm qualified enough to help you. I'm also a C# starter. Ah, if my solution helped you, please click on the accept solution button. Thanks!
Kay Pee Singh 29-Sep-12 16:03pm
   
yeah i already clicked on the accept solution u have not seen ??
Kay Pee Singh 29-Sep-12 16:04pm
   
can u tell me how can i store this screenshot picture in my database ?????
supernorb 30-Sep-12 8:21am
   
You can store the paths to your image files. That's the easiest way to do. Another way is to convert your Image objects to arrays of byte using seriralization, then insert these arrays of byte into some column of binary/varbinary/image in your table. This is also not too difficult, and you can easily search for that on the internet! It takes about 10 lines of code.
I have updated my solution with an addition about the 2 functions converting between Image and byte[], you can see them as an example.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

use LinqToSql class model
much easier way
  Permalink  
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. :)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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.
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.
  Permalink  
v4

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
Web01 | 2.8.160426.1 | Last Updated 30 Sep 2012
Copyright © CodeProject, 1999-2016
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