用C#插入图像哈希 [英] Image Hash inserting in C#
问题描述
<$>当我上传图像哈希也生成与该图像我存储图像中的数据库,但问题是我如何将哈希存储在database.Below是我的代码。 p $ p $
private void FirstpictureBox_Click(object sender,EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title =选择第一张图片;
ofd.Filter =Image File(*。png; *。jpg; *。bmp; *。gif)| * .png; *。jpg; *。bmp; *。gif;
if(ofd.ShowDialog()== DialogResult.OK)
{
FirstpictureBox.Image = new Bitmap(ofd.FileName);
byte [] imgBytes = new byte [0];
//将图像转换为字节数组
imgBytes =(byte [])converter.ConvertTo(FirstpictureBox.Image,imgBytes.GetType());
//从图像字节计算SHA哈希字符串
string hash = ComputeHashCode(imgBytes);
私有字符串ComputeHashCode(byte [] imgBytes)
{
//计算散列字节
byte [] hash = shaEncryptor.ComputeHash( imgBytes);
//将散列字节转换为字符串表示形式
返回Convert.ToBase64String(hash);
$ b public void ImageHash(string hash)
{
}
private void SaveRecord()
{
ImageHash(hash);
字符串connString = ConfigurationManager.ConnectionStrings [dbx]。ConnectionString;
string cmdString =INSERT INTO users(img1,hash)VALUES(@ firstimage,@ hash);
using(OleDbConnection con = new OleDbConnection(connString))
{
using(OleDbCommand cmd = new OleDbCommand(cmdString,con))
{
con.Open( );
cmd.Parameters.AddWithValue(@ firstimage,savePhoto());
cmd.Parameters.AddWithValue(@ hash,hash);
cmd.ExecuteNonQuery();
$ b
当我在SaveRecord()函数中调用ImageHash(hash)函数,它给了我一个error.hash在当前context.how中不存在,我可以修复这个问题。
在类级别声明您的 hash
变量(也就是说,在任何函数或方法之外)。
现在,您已经在函数/方法级别声明了字符串,这意味着只有 FirstpictureBox_Click()
将可以访问它。
字符串散列;只需将该声明移出该方法即可。
$ b $ private void FirstpictureBox_Click(object sender,EventArgs e)
{
...您的代码...
hash = ComputeHashCode(imgBytes);
}
When i upload an image hash is also generated with that image i store image in db but problem is how can i store hash in database.Below is my code.
private void FirstpictureBox_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "Select First Image";
ofd.Filter = "Image File(*.png;*.jpg;*.bmp;*.gif)|*.png;*.jpg;*.bmp;*.gif";
if (ofd.ShowDialog() == DialogResult.OK)
{
FirstpictureBox.Image = new Bitmap(ofd.FileName);
byte[] imgBytes = new byte[0];
//convert image to byte array
imgBytes = (byte[])converter.ConvertTo(FirstpictureBox.Image, imgBytes.GetType());
//compute SHA hash string from image bytes
string hash = ComputeHashCode(imgBytes);
}
}
private string ComputeHashCode(byte[] imgBytes)
{
//Compute hash bytes
byte[] hash = shaEncryptor.ComputeHash(imgBytes);
//Convert hash bytes to string representation
return Convert.ToBase64String(hash);
}
public void ImageHash(string hash)
{
}
private void SaveRecord()
{
ImageHash(hash);
string connString = ConfigurationManager.ConnectionStrings["dbx"].ConnectionString;
string cmdString = "INSERT INTO users (img1,hash) VALUES (@firstimage,@hash)";
using (OleDbConnection con = new OleDbConnection(connString))
{
using (OleDbCommand cmd = new OleDbCommand(cmdString, con))
{
con.Open();
cmd.Parameters.AddWithValue("@firstimage", savePhoto());
cmd.Parameters.AddWithValue("@hash", hash);
cmd.ExecuteNonQuery();
}
}
}
When i call ImageHash(hash) function in SaveRecord() function it give me an error.hash doesn't exist in current context.how can i fix this problem.
Declare your hash
variable at class level instead (that is, outside any function or method).
As it is now, you have declared the string at function/method level, which means only FirstpictureBox_Click()
will have access to it. Just move the declaration out of the method and you are good to go.
string hash;
private void FirstpictureBox_Click(object sender, EventArgs e)
{
...your code...
hash = ComputeHashCode(imgBytes);
}
这篇关于用C#插入图像哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!