項(xiàng)目上要求把圖片存入數(shù)據(jù)庫(kù),有時(shí)候圖片是分開(kāi)的但是要在上傳時(shí)合并(上下合并,新傳的圖片放后面,特殊需要呵呵)。Google了半天網(wǎng)上有圖片存入數(shù)據(jù)庫(kù)的示例,便是合并的就基本沒(méi)用,有也是少量其它操作方式的代碼片段。結(jié)合網(wǎng)上思路,研究出圖片合并的比較完整的代碼如下:
代碼類(lèi)型:C# 2.0
說(shuō)明:此文本站原創(chuàng),轉(zhuǎn)載請(qǐng)注明出處
上傳文件使用的是VS2005自帶的上傳控件
省略相關(guān)上傳的及其它不怎么太有關(guān)系的代碼,上傳代碼看MSDN幫助,比較完整
合并流程:上傳一張,存入數(shù)據(jù)庫(kù),然后再上傳一張和指定存入數(shù)據(jù)庫(kù)的圖片合并后更新數(shù)據(jù)庫(kù)字段view plaincopy to clipboardPRint?
//fu_SelectImage(VS2005自帶上傳控件)
byte[] FileByteArray = fu_SelectImage.FileBytes;//圖象文件臨時(shí)儲(chǔ)存Byte數(shù)組
Stream StreamObject = fu_SelectImage.FileContent;//建立數(shù)據(jù)流對(duì)像
Response.Write("文件長(zhǎng)度為:"+ FileLength + "文件名為:" + fu_SelectImage.FileName + "文件類(lèi)型為:" + fu_SelectImage.GetType().Name); //用來(lái)自己看看的。
//定義一個(gè)新的byte[]用來(lái)存放數(shù)據(jù)庫(kù)中的圖片byte[]
byte[] FileByteOldArray = new byte[1];
DataHandle dh = new DataHandle(); //這個(gè)是朋友寫(xiě)好的數(shù)據(jù)庫(kù)操作類(lèi)直接拿來(lái)用上了,主要是讀取數(shù)據(jù)庫(kù)指定字段內(nèi)容。
dh.TargetTableName = "ImageStore"; //存圖片的數(shù)據(jù)表
dh.DbConditionAdd("ImageID",5); //圖片的ID(ID可以從網(wǎng)址參數(shù)處獲得getimage.aspx?id=5)
SqlDataReader rd = dh.ExecuteReader();
if (rd.Read())
{
FileByteOldArray = (byte[])rd["ImageData"]; //從數(shù)據(jù)庫(kù)取出圖片的byte[],數(shù)據(jù)庫(kù)此字段為Image類(lèi)型
}
rd.Close();
dh.Close();
//將圖像的字節(jié)數(shù)組放入內(nèi)存流
MemoryStream oldms = new MemoryStream(FileByteOldArray); //存放數(shù)據(jù)庫(kù)的圖片字節(jié)數(shù)組內(nèi)存流
MemoryStream newms = new MemoryStream(); //用來(lái)存放合并后的內(nèi)存流
//合并位圖,這個(gè)部分是關(guān)鍵,從這里也可以演化左右合并之類(lèi)的。當(dāng)然圖片的縮小也差不多思路
Bitmap b1 = new Bitmap(oldms); //oldms老的存在數(shù)據(jù)庫(kù)的內(nèi)存流
Bitmap b2 = new Bitmap(StreamObject); //StreamObject新上傳的圖片流
Bitmap b = new Bitmap(b1.Width > b2.Width ? b1.Width : b2.Width, b1.Height + b2.Height+1);
Graphics g = Graphics.FromImage(b);
g.DrawImage(b1,0,0);
g.DrawImage(b2, 0, b1.Height+1);//上下合并,把新圖片放到老圖片的下面,這個(gè)1主要是為了有1px的間隔,也可以去的。
//把合并后的位圖保存到內(nèi)存流中
b.Save(newms, ImageFormat.Jpeg);
//從合并后圖片的內(nèi)存流中取得存放數(shù)據(jù)庫(kù)所需的相關(guān)參數(shù)
byte[] FileByteNewArray = newms.GetBuffer();
FileLength = FileByteNewArray.Length;
//釋放資源
oldms.Dispose();
newms.Dispose();
b1.Dispose();
b2.Dispose();
b.Dispose();
g.Dispose();
//建立SQL Server鏈接
SqlConnection Con = new SqlConnection("Data Source=Localhost;Initial Catalog=MapPointManageDemo;User ID=sa;Pwd=sa;");
//String SqlCmd = "INSERT INTO ImageStore (ImageData, ImageContentType,ImageDescription, ImageSize) valueS (@Image, @ContentType,@ImageDescription, @ImageSize)";
String SqlCmd = "UPDATE ImageStore SET ImageData = @Image, ImageSize = ImageSize + @ImageSize WHERE ImageID = 5";
SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);
CmdObj.Parameters.Add("@Image", SqlDbType.Binary, FileLength).Value = FileByteNewArray;//保存圖片byte[]
//CmdObj.Parameters.Add("@ContentType", SqlDbType.VarChar, 50).Value = "image/jpeg"; //記錄文件類(lèi)型
//CmdObj.Parameters.Add("@ImageDescription", SqlDbType.VarChar, 200).Value = txtDescription.Text;//把其它單表數(shù)據(jù)記錄上傳
CmdObj.Parameters.Add("@ImageSize", SqlDbType.BigInt, 8).Value = FileLength;//記錄文件長(zhǎng)度,讀取時(shí)使用
Con.Open();
CmdObj.ExecuteNonQuery();
Con.Close();
//fu_SelectImage(VS2005自帶上傳控件)
byte[] FileByteArray = fu_SelectImage.FileBytes;//圖象文件臨時(shí)儲(chǔ)存Byte數(shù)組
Stream StreamObject = fu_SelectImage.FileContent;//建立數(shù)據(jù)流對(duì)像
Response.Write("文件長(zhǎng)度為:"+ FileLength + "文件名為:" + fu_SelectImage.FileName + "文件類(lèi)型為:" + fu_SelectImage.GetType().Name); //用來(lái)自己看看的。
//定義一個(gè)新的byte[]用來(lái)存放數(shù)據(jù)庫(kù)中的圖片byte[]
byte[] FileByteOldArray = new byte[1];
DataHandle dh = new DataHandle(); //這個(gè)是朋友寫(xiě)好的數(shù)據(jù)庫(kù)操作類(lèi)直接拿來(lái)用上了,主要是讀取數(shù)據(jù)庫(kù)指定字段內(nèi)容。
dh.TargetTableName = "ImageStore"; //存圖片的數(shù)據(jù)表
dh.DbConditionAdd("ImageID",5); //圖片的ID(ID可以從網(wǎng)址參數(shù)處獲得getimage.aspx?id=5)
SqlDataReader rd = dh.ExecuteReader();
if (rd.Read())
{
FileByteOldArray = (byte[])rd["ImageData"]; //從數(shù)據(jù)庫(kù)取出圖片的byte[],數(shù)據(jù)庫(kù)此字段為Image類(lèi)型
}
rd.Close();
dh.Close();
//將圖像的字節(jié)數(shù)組放入內(nèi)存流
MemoryStream oldms = new MemoryStream(FileByteOldArray); //存放數(shù)據(jù)庫(kù)的圖片字節(jié)數(shù)組內(nèi)存流
MemoryStream newms = new MemoryStream(); //用來(lái)存放合并后的內(nèi)存流
//合并位圖,這個(gè)部分是關(guān)鍵,從這里也可以演化左右合并之類(lèi)的。當(dāng)然圖片的縮小也差不多思路
Bitmap b1 = new Bitmap(oldms); //oldms老的存在數(shù)據(jù)庫(kù)的內(nèi)存流
Bitmap b2 = new Bitmap(StreamObject); //StreamObject新上傳的圖片流
Bitmap b = new Bitmap(b1.Width > b2.Width ? b1.Width : b2.Width, b1.Height + b2.Height+1);
Graphics g = Graphics.FromImage(b);
g.DrawImage(b1,0,0);
g.DrawImage(b2, 0, b1.Height+1);//上下合并,把新圖片放到老圖片的下面,這個(gè)1主要是為了有1px的間隔,也可以去的。
//把合并后的位圖保存到內(nèi)存流中
b.Save(newms, ImageFormat.Jpeg);
//從合并后圖片的內(nèi)存流中取得存放數(shù)據(jù)庫(kù)所需的相關(guān)參數(shù)
byte[] FileByteNewArray = newms.GetBuffer();
FileLength = FileByteNewArray.Length;
//釋放資源
oldms.Dispose();
newms.Dispose();
b1.Dispose();
b2.Dispose();
b.Dispose();
g.Dispose();
//建立SQL Server鏈接
SqlConnection Con = new SqlConnection("Data Source=Localhost;Initial Catalog=MapPointManageDemo;User ID=sa;Pwd=sa;");
//String SqlCmd = "INSERT INTO ImageStore (ImageData, ImageContentType,ImageDescription, ImageSize) valueS (@Image, @ContentType,@ImageDescription, @ImageSize)";
String SqlCmd = "UPDATE ImageStore SET ImageData = @Image, ImageSize = ImageSize + @ImageSize WHERE ImageID = 5";
SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);
CmdObj.Parameters.Add("@Image", SqlDbType.Binary, FileLength).Value = FileByteNewArray;//保存圖片byte[]
//CmdObj.Parameters.Add("@ContentType", SqlDbType.VarChar, 50).Value = "image/jpeg"; //記錄文件類(lèi)型
//CmdObj.Parameters.Add("@ImageDescription", SqlDbType.VarChar, 200).Value = txtDescription.Text;//把其它單表數(shù)據(jù)記錄上傳
CmdObj.Parameters.Add("@ImageSize", SqlDbType.BigInt, 8).Value = FileLength;//記錄文件長(zhǎng)度,讀取時(shí)使用
Con.Open();
CmdObj.ExecuteNonQuery();
Con.Close();附帶點(diǎn)其它的圖片操作模式
來(lái)源于:http://topic.csdn.net/t/20050225/15/3806327.htmlview plaincopy to clipboardprint?
//處理圖片大小到指定尺寸,返回值為一個(gè)Image對(duì)象,使用Image對(duì)象的Save方法就可以保存該圖片
//詳細(xì)用法查MSDN
private System.Drawing.Image PhotoSizeChange(string strPhoto)
{
//strPhoto是原來(lái)的圖片文件所在的物理路徑
//處理圖片功能
System.Drawing.Image image = new Bitmap(strPhoto);//得到原圖
//創(chuàng)建指定大小的圖
System.Drawing.Image newImage = image.GetThumbnailImage(指定寬(像素值 int), 指定高(像素值 int), null, new IntPtr());
Graphics g=Graphics.FromImage(newImage);
//將原圖畫(huà)到指定的圖上
g.DrawImage(newImage,X,Y, newImage.Width, newImage.Height);
g.Dispose();
return newImage;
}
http://blog.breakn.net/article.asp?id=325
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注