因为项目需要动态改变letter模板的logo,现在的思路是将logo图片放在文件夹里,要用一个方法将图片取出来并放到dataSet中,在用存储过程将dataSet的值保存到dataBase 中,再将dataBase的图片放到letter模板上.
我现在的问题是如何将图片(gif,bmp)转成可以存放在dataSet中的byte数组,或者其它数据格式,并且将这个数据格式在letter 模板上还原出来.问题比较麻烦,先谢谢大家!
我现在的问题是如何将图片(gif,bmp)转成可以存放在dataSet中的byte数组,或者其它数据格式,并且将这个数据格式在letter 模板上还原出来.问题比较麻烦,先谢谢大家!
去查查,我不记得了饿
FileStream fs = new FileStream(@"c:/图片1.gif", FileMode.Open);
int lenth = int.Parse(fs.Length.ToString());
byte[] barr = new byte[lenth];
fs.Read(barr,0,lenth);
//Open file into a filestream and
//read data in a byte array.
byte[] ReadFile(string sPath)
{
//Initialize byte array with a null value initially.
byte[] data = null; //Use FileInfo object to get file size.
FileInfo fInfo = new FileInfo(sPath);
long numBytes = fInfo.Length; //Open FileStream to read file
FileStream fStream = new FileStream(sPath, FileMode.Open,
FileAccess.Read); //Use BinaryReader to read file stream into byte array.
BinaryReader br = new BinaryReader(fStream); //When you use BinaryReader, you need to
//supply number of bytes to read from file.
//In this case we want to read entire file.
//So supplying total number of bytes.
data = br.ReadBytes((int)numBytes);
return data;
}2. 保存图片到数据库private void cmdSave_Click(object sender, EventArgs e)
{ //Read Image Bytes into a byte array
byte[] imageData = ReadFile(txtImagePath.Text); //Initialize SQL Server Connection
SqlConnection CN = new SqlConnection(txtConnectionString.Text); //Set insert query
string qry = "insert into ImagesStore (OriginalPath,ImageData) _
values(@OriginalPath, @ImageData)"; //Initialize SqlCommand object for insert.
SqlCommand SqlCom = new SqlCommand(qry, CN); //We are passing Original Image Path and
//Image byte data as sql parameters.
SqlCom.Parameters.Add(new SqlParameter("@OriginalPath",
(object)txtImagePath.Text));
SqlCom.Parameters.Add(new SqlParameter("@ImageData",
(object)imageData)); //Open connection and execute insert query.
CN.Open();
SqlCom.ExecuteNonQuery();
CN.Close(); //Close form and return to list or images.
this.Close();
}
3. 读取图片数据并写到文件里 SqlConnection con = new SqlConnection("Server=Darkover;uid=<username>;pwd=<strong password>;database=northwind");
SqlDataAdapter da = new SqlDataAdapter("Select * From MyImages", con);
SqlCommandBuilder MyCB = new SqlCommandBuilder(da);
DataSet ds = new DataSet("MyImages"); byte[] MyData= new byte[0];
da.Fill(ds, "MyImages");
DataRow myRow;
myRow=ds.Tables["MyImages"].Rows[0];
MyData = (byte[])myRow["imgField"];
int ArraySize = new int();
ArraySize = MyData.GetUpperBound(0); FileStream fs = new FileStream(@"C:\winnt\Gone Fishing2.BMP", FileMode.OpenOrCreate, FileAccess.Write);
fs.Write(MyData, 0,ArraySize);
fs.Close();