使用C#以二进制格式转换图像 [英] Convert image in binary format using c#

查看:136
本文介绍了使用C#以二进制格式转换图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我想使用c#.net 2.0和sql server 2005将图像转换为二进制格式.请告诉我我该怎么做.

谢谢
Rakesh Kumar

Hi,
I want to convert image in to binary format using c#.net 2.0 and sql server 2005.please tell me how i will i do this.

thanks
Rakesh Kumar

推荐答案

我想到尝试 ^ ].所以我做到了,并且被这么多链接淹没了:
http://www.c-sharpcorner.com/UploadFile/hscream/ImagetoBinary02132007164649PM/ImagetoBinary.aspx [ ^ ]
http://stackoverflow.com/questions/1373032/c-howto-convert-image-到二进制 [ ^ ]
http://www.dotnetspider.com/resources/6150-Convert-Image-binary- format.aspx [ ^ ]

选择任何一个!
I thought of tryingGoogle[^]. So i did and was flooded with so many links:
http://www.c-sharpcorner.com/UploadFile/hscream/ImagetoBinary02132007164649PM/ImagetoBinary.aspx[^]
http://stackoverflow.com/questions/1373032/c-howto-convert-image-to-binary[^]
http://www.dotnetspider.com/resources/6150-Convert-Image-binary-format.aspx[^]

Pick any!


您应该这样做:

使用system.io;
使用system.data.sqlclient;

公共局部类Form1:Form
{
SqlConnection con =新的SqlConnection();
字符串_path;


//将图像转换为二进制文件并保存在数据库中
private void button1_Click(对象发送者,EventArgs e)
{
如果(openFileDialog1.ShowDialog()== DialogResult.OK)
{

_path = openFileDialog1.FileName;


InsertInSQL(_path);


}

}

私有void InsertInSQL(string _path)
{
con.ConnectionString = Pic.Properties.Settings.Default.ConnectionS;
字符串strQ =插入dbo.PicTBL(Pic)values(@p)";
SqlCommand命令=新的SqlCommand(strQ,con);
command.Parameters.AddWithValue("@ p",ImageToBinary(_path));
con.Open();
command.ExecuteNonQuery();
con.Close();
}

公共静态字节[] ImageToBinary(字符串_path)
{
FileStream fS =新的FileStream(_path,FileMode.Open,FileAccess.Read);
byte [] b =新的byte [fS.Length];
fS.Read(b,0,(int)fS.Length);
fS.Close();
返回b;
}






//将Binary转换为imge并保存在文件夹中



private void button1_Click_1(对象发送者,EventArgs e)
{


DataTable dt = Rimage();

foreach(dt.Rows中的DataRow行)
{
byte [] b =(byte [])row ["Pic"];
图片img = BinaryToImage(b);
img.Save("D:\\ NewFolder \\" + row ["ID"].ToString()+".jpg");
}


}

私有图像BinaryToImage(byte [] b)
{
如果(b == null)
返回null;


MemoryStream memStream =新的MemoryStream();
memStream.Write(b,0,b.Length);

返回Image.FromStream(memStream);

}

私有DataTable Rimage()
{
con.ConnectionString = Pic.Properties.Settings.Default.ConnectionS;
SqlCommand cmd =新的SqlCommand();
cmd.CommandText =从dbo.PicTBL中选择*";
cmd.Connection = con;
SqlDataAdapter adp =新的SqlDataAdapter(cmd);
DataTable dt = new DataTable();
con.Open();
adp.Fill(dt);

return dt;

}



}
you shuold do this:

using system.io;
using system.data.sqlclient;

public partial class Form1 : Form
{
SqlConnection con = new SqlConnection();
string _path;


//convert Image to binary and save in DB
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{

_path = openFileDialog1.FileName;


InsertInSQL(_path);


}

}

private void InsertInSQL(string _path)
{
con.ConnectionString = Pic.Properties.Settings.Default.ConnectionS;
string strQ = "insert into dbo.PicTBL(Pic)values(@p)";
SqlCommand command = new SqlCommand(strQ,con);
command.Parameters.AddWithValue("@p",ImageToBinary(_path));
con.Open();
command.ExecuteNonQuery();
con.Close();
}

public static byte[] ImageToBinary(string _path)
{
FileStream fS = new FileStream(_path, FileMode.Open, FileAccess.Read);
byte[] b = new byte[fS.Length];
fS.Read(b, 0, (int)fS.Length);
fS.Close();
return b;
}






//Convert Binary to imge and save in a folder



private void button1_Click_1(object sender, EventArgs e)
{


DataTable dt = Rimage();

foreach (DataRow row in dt.Rows)
{
byte[] b = (byte[])row["Pic"];
Image img = BinaryToImage(b);
img.Save("D:\\NewFolder\\" + row["ID"].ToString() + ".jpg");
}


}

private Image BinaryToImage(byte[] b)
{
if (b == null)
return null;


MemoryStream memStream = new MemoryStream();
memStream.Write(b, 0, b.Length);

return Image.FromStream(memStream);

}

private DataTable Rimage()
{
con.ConnectionString = Pic.Properties.Settings.Default.ConnectionS;
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select * from dbo.PicTBL";
cmd.Connection = con;
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
con.Open();
adp.Fill(dt);

return dt;

}



}


这篇关于使用C#以二进制格式转换图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆