数据库加图片

附:代码

  private System.Windows.Forms.PictureBox pictureBox1;
  private System.Data.SqlClient.SqlCommand cmdExecute;
  private System.Data.SqlClient.SqlConnection connEmployee;
  private Bitmap image;
  private byte[] buffer;

private void ShowMyImage(string fileToDisplay, int xSize, int ySize)
  {
   // Sets up an image object to be displayed.
   if (image != null)
   {
    image.Dispose();
   }

   // Stretches the image to fit the pictureBox.
   pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage ;
   image = new Bitmap(fileToDisplay);
   pictureBox1.ClientSize = new Size(xSize, ySize);
   pictureBox1.Image = (Image) image ;
  }

  private void button1_Click(object sender, System.EventArgs e)
  {
   OpenFileDialog openFileDialog = new OpenFileDialog();
   openFileDialog.Title = "请选择照片";
   openFileDialog.ReadOnlyChecked = true;
   openFileDialog.ShowReadOnly = true;
   openFileDialog.Filter = "所有图片文件|*.*|JPEG(*.JPG;*.JPEG;*.JPEe;*.JFIF)|*.JPG;*.JPEG;*.JPEe;*.JFIF|位图文件(*.BMP)|*.BMP|GIF(*.GIF)|*.GIF";

   string pictureName = "";
   if (openFileDialog.ShowDialog() == DialogResult.OK)
   {
    pictureName = openFileDialog.FileName;
    this.ShowMyImage(pictureName, 112, 130);
   }

   if (pictureName != "")
   {
    FileStream stream = new FileStream(pictureName, FileMode.Open, FileAccess.Read);
    buffer = new byte[stream.Length];
    stream.Read(buffer, 0, (int)stream.Length);
    stream.Close();
   }

   this.cmdExecute.CommandText = @"INSERT INTO Photo_Test(Photo) values(@Photo)";
   this.cmdExecute.Parameters.Add("@Photo", SqlDbType.Image, buffer.Length, "Photo");
   this.cmdExecute.Parameters["@Photo"].Value = buffer;
   if (this.connEmployee.State == ConnectionState.Closed)
    this.connEmployee.Open();
   this.cmdExecute.ExecuteNonQuery();
  }

  private void button2_Click(object sender, System.EventArgs e)
  { 
   SqlDataAdapter da = new SqlDataAdapter("Select * from Photo_Test WHERE ID = 20", this.connEmployee);
   DataSet ds = new DataSet();
   da.Fill(ds);
   DataTable dt = ds.Tables[0];
   byte[] b = (byte[])dt.Rows[0][1];

   if(b.Length > 0)
   {
    MemoryStream stream = new MemoryStream(b, true);
    stream.Write(b, 0, b.Length);
     
    image=new Bitmap(stream);
    
    stream.Close();
    this.pictureBox1.Image = image;
   }
  }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
图片密存储到数据库是一种常见的做法,有助于保护图像的安全性和隐私。在Java中,可以通过以下步骤实现图片密存储到数据库。 步骤1:选择适合图像密的算法。常见的图像密算法包括AES(高级密标准)和RSA(非对称密算法)。根据需要选择适合的算法。 步骤2:读取图像文件。可以使用Java的IO类库中的File类和BufferedImage类来读取图像文件。通过指定图像文件的路径,可以将图像文件载到内存中。 步骤3:将图像文件转换为字节数组。使用Java的IO类库将图像文件转换为字节数组。可以使用InputStream类的read()方法逐个字节读取图像文件,并将其存储在字节数组中。 步骤4:使用选定的密算法对图像字节数组进行密。根据所选的密算法,在Java中使用相应的密类库对图像字节数组进行密。例如,如果选择AES算法,则可以使用Java密技术(javax.crypto)提供的相应类和方法对字节数组进行密。 步骤5:将密后的图像字节数组存储到数据库。使用Java中的数据库访问技术(如JDBC)连接到数据库,并将密后的图像字节数组存储到指定的数据库表中。可以使用预定义的方法(如PreparedStatement的setBytes()方法)将字节数组作为参数传递给SQL INSERT语句。 步骤6:检索和解密存储在数据库中的图片。使用数据库查询语句(如SELECT语句)从数据库中检索密图像的字节数组。然后,使用相同的密算法和密钥对字节数组进行解密,并将解密后的字节数组转换为图像文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值