Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I write the code add data to mysql using C# with structure of image is longblob.
And then I make a php script to display it on web? But I can convert longblob to image. I try to convert binary to image but ít not work.How can I do this?
Here is my code:
C#
private void btn_open_Click(object sender, EventArgs e)
{
    OpenFileDialog dlg = new OpenFileDialog();
    dlg.Filter = "JPG Files(*.jpg)|*.jpg|PNG Files(*.png)|*.png|ALL Files(*.*)|*.*";
    if (dlg.ShowDialog() == DialogResult.OK)
    {
        string picPath = dlg.FileName.ToString();
        txt_path.Text = picPath;
        pictureBox1.ImageLocation = picPath;
    }
}
private void btn_add_Click(object sender, EventArgs e)
{
    try
    {
        // conver to byte
        byte[] imageBt = null;
        FileStream fstream = new FileStream(this.txt_path.Text, FileMode.Open, FileAccess.Read);
        BinaryReader br = new BinaryReader(fstream);
        imageBt = br.ReadBytes((int)fstream.Length);
        //kết nối database
        string constring = "Server=localhost;Database=luan_van;Port=3306;User ID=root;Password=";
        string Query = "INSERT into tt_nhanvien (Card_ID,Ma_nv,Ten,Phong_ban,Chuc_vu,Hinh) value ('"+this.txt_ID.Text +"','"+this.txt_ma.Text+"','"+this.txt_ten.Text+"','"+this.txt_pban.Text+"','"+this.txt_chvu.Text+"',@image);";
        MySqlConnection conDataBase = new MySqlConnection(constring);
        MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
        MySqlDataReader myReader;
        try
        {
            conDataBase.Open();
            cmdDataBase.Parameters.Add(new MySqlParameter("@image", imageBt));
            myReader = cmdDataBase.ExecuteReader();
            MessageBox.Show("SAVE");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
    catch
    {

    }
}

and PHP code:
PHP
<?php
	error_reporting(0);
//$db= mysql_connect("localhost","root","root");
	$db= mysql_connect("localhost","root","");
	if(!$db)
	{
		echo "KHÔNG THỂ KẾT NỐI DATABASE";
		exit;
	}
	//chon csdl
	$db_selected = mysql_select_db("luan_van",$db);
	if(!$db_selected)
	{
		die("Không thể sử dụng DATABASE: ".mysql_error());
	}
	$Ten=$_REQUEST["Ten"];// lấy được tên nhân viên mà người dùng chọn bằng cách lick chuột
	// truy vấn csdl
	$sql= "SELECT * FROM tt_nhanvien WHERE Ten LIKE '$Ten'"; // chỉ chọn đúng tên laptop request
	$result = mysql_query($sql);
	//xuat dữ liệu
	if(mysql_num_rows($result)<>0)
	{
		echo "<table width='800' border='1' align='center'cellpadding='0' cellspacing='0' border='1' style='border-collapse:collapse #FF6600'>";
		while($row = mysql_fetch_row($result))
		{
			$Card_ID= $row[0];
			$Ma_nv = $row[1];
			$Ten = $row[2];
			$Phong_ban = $row[3];
			$Chuc_vu = $row[4];
			$Hinh = "nhan_vien/".$row[5];
			$Hinh = "<img src ='$Hinh' alt='$Ten'>";
			$image_data=file_get_contents($Hinh);
			$encoded_image=base64_encode($image_data);
			$decoded_image=base64_decode($encoded_image);
			echo "<tr><td align='center' colspan='2' bgcolor='#FFEEE6' >$Ten</td></tr>";
			
			echo"<tr>";
			echo"<td align='center' valign='middle'>$decoded_image</td>";
			echo "<td>";
			echo "<table align='center'";
				echo "<tr><td> Card ID:$Card_ID</td></tr>";
				echo "<tr><td> Tên:$Ten</td></tr>";
				echo "<tr><td> Phòng ban:$Phong_ban</td></tr>";
				echo "<tr><td> Mã nhân viên:$Ma_nv</td></tr>";
				echo "<tr><td> Chức vụ:$Chuc_vu</td></tr>";
			echo "</table>";
			echo "</td>";
			echo "<tr align='right'><td><a href='thong_tin_nv.php'> Quay về</a></td></tr>";
			echo "</tr>";	
		}
		echo "</table>";
	}
?>
Posted

1 solution

Convert those bytes in your database to base64 encoded string and display it thus <img src="data:image/jpeg;base64,/9ja/..." />.

You don't need the file_get_contents because you are not trying to read the image data from a URI, you already have the image data.

I believe $row[5] holds the image data so the code should be


PHP
$image_data = $row[5];
$encoded_image = base64_encode($image_data);
//You dont need to decode it again.

$Hinh = "<img src='data:image/jpeg;base64,{$encoded_image}' alt=\"$Ten\">";

//and you echo $Hinh
echo $Hinh;
</img>
 
Share this answer
 
v3
Comments
Member 10390715 4-Dec-14 9:05am    
You're very nice. Thank you very much!

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