如何使用c#.net窗口应用程序将屏幕快照存储在数据库中? [英] how can store screenshot in database using c#.net window application ?
本文介绍了如何使用c#.net窗口应用程序将屏幕快照存储在数据库中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将屏幕快照图片存储在数据库中,但未将其加载到数据库中.
当我执行此程序时,这将显示错误,这是GDI +中发生的一般错误.我怎样才能消除此错误,也要在相应的时间段存储多个屏幕截图,有人可以帮助我.............?
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();
}
推荐答案
使用LinqToSql类模型
更简单的方法
use LinqToSql class model
much easier way
嘿,我发现了您的问题!
仅仅因为文件i.jpeg是第一次创建的,而您没有更改i(例如,如果它代表您的情况,则增加i),我看到您将i声明为字符串,这意味着您可能没有想要为i生成下一个数值作为数字?
我将i声明为整数,并且在拍摄每张快照后,我将i都加1,这样就可以很好地工作了.
希望你能理解你的问题!
PS:如果您再次不小心,将来可能仍然会出现此问题.我的意思是您应该有某种方式来避免文件名重复的问题.
保存并加载图片
这是2个用于在Image和byte []之间进行转换的函数,当将数据插入数据库时,您应该首先将数据作为byte [],并且在加载图像数据时,会将其作为byte [],因此将它们转换为图像,然后才能在图片框中显示它们.
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:我尚未测试过代码,但我认为它应该可以工作.尝试一下.
PS: I''ve not tested the code, but I think it should work. Try yourself.
这篇关于如何使用c#.net窗口应用程序将屏幕快照存储在数据库中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文