圖片縮放及改變資料庫中的容量(BLOB),降低品質等專案代碼.
去年的事情吧, 有一天,我司DBA人員請我幫忙處理一件事情. 具體需求如下:
資料庫: ORACLE9I +ORACLE10G
圖片存放字段類型: BLOB 圖片存放空間現在是相當的大了, 因客戶端用戶上傳資料時,大到幾十M,上百M的圖片,小到幾十K的圖片都有, 注意:客戶前端所用工具為PB的視窗程式上傳, 最開始時,沒有控制大小, 最近才控制的. 而現在資料庫光存放圖片的表的容就是500多G了,不加其它1354表的容.
問題:所以每次備份及轉換時,都是要命.
目標:改變圖片的容量, 在保證1024*768的清晰程度下,盡可能小的改變資料庫容量.
具體過程:1.取資料, 2.轉換BLOG成byte[],3.轉換圖片大小,4.調整圖片品質,5.更新資料字段.
具體核心代碼:
1. 取資料:
string conn_str = System.Configuration.ConfigurationSettings.AppSettings["picture"].Trim();
OracleDataAdapter da = new OracleDataAdapter();
DataSet ds = new DataSet();
OracleConnection conn = new OracleConnection(conn_str);
OracleCommand cmd = new OracleCommand("SELECT FACT_NO,ITEM_SEQ,BLOB_DATA,EXP_NAME,dbms_lob.getlength(blob_data) AS QTY from style_picture where dbms_lob.getlength(blob_data) / 1024 > 300 ", conn);
da.SelectCommand = cmd;
conn.Open();
OracleDataReader odr=cmd.ExecuteReader();
2. .轉換BLOG成byte[]:
while (odr.Read())
{
if(!odr["BLOB_DATA"].ToString().Equals(""))
{
fact_no = odr["FACT_NO"].ToString().Trim();
item_seq = odr["ITEM_SEQ"].ToString().Trim();
pictype = odr["EXP_NAME"].ToString().Trim();
byte[] imgbyte = new byte[0];
imgbyte = (byte[])odr["BLOB_DATA"];
3. 轉換圖片大小.格式:
System.IO.MemoryStream ms = new MemoryStream(imgbyte);
System.IO.MemoryStream mspost = new MemoryStream();
//string length=imgbyte.Length.ToString();
System.Drawing.Bitmap bmtest = new System.Drawing.Bitmap(System.Drawing.Image.FromStream(ms));
// int hash=Convert.ToInt32(ds.Tables[0].Rows[i]["QTY"].ToString().Trim());
// if(hash>400&&hash<=600)
// {
// newwidth=Convert.ToInt32(Math.Round(bmtest.Width*0.75));
// newheigth=Convert.ToInt32(Math.Round(bmtest.Height*0.75));
// }
// if(hash>600&&hash<=800)
// {
// newwidth=Convert.ToInt32(Math.Round(bmtest.Width*0.6));
// newheigth=Convert.ToInt32(Math.Round(bmtest.Height*0.6));
// }
// if(hash>800&&hash<=1000)
// {
// newwidth=Convert.ToInt32(Math.Round(bmtest.Width*0.5));
// newheigth=Convert.ToInt32(Math.Round(bmtest.Height*0.5));
// }
// if(hash>1000)
// {
// newwidth=Convert.ToInt32(Math.Round(bmtest.Width*0.4));
// newheigth=Convert.ToInt32(Math.Round(bmtest.Height*0.4));
// }
4.調整圖片品質:
bmtest=null;
System.Drawing.Bitmap bm = new System.Drawing.Bitmap(System.Drawing.Image.FromStream(ms), newwidth,newheigth);
//System.Drawing.Bitmap bm = new System.Drawing.Bitmap(System.Drawing.Image.FromFile(@"C:\Inetpub\wwwroot\WebSite\images\11.jpg"), 1024, 768);
//bm.Save(@"C:\Inetpub\wwwroot\WebSite\images\11.jpg", System.Drawing.Imaging.ImageFormat.Gif);
//調整品質.
EncoderParameters ep = new EncoderParameters(1);
ep.Param[0] = new EncoderParameter(Encoder.Quality, 25L);
bm.Save(mspost, GetEncoder(pictype),ep);
byte[] postbyte = new byte[0];
postbyte = mspost.ToArray();
5.更新資料字段:
string sql = "UPDATE style_picture SET BLOB_DATA= :BLOB_DATA,pict_path='"+DateTime.Now.ToString("yyyyMMddhhmmss").Trim()+"' WHERE fact_no='" + fact_no + "' and item_seq='" + item_seq + "' ";
OracleCommand com1 = new OracleCommand(sql, conn);
com1.Parameters.Add(":BLOB_DATA", OracleType.Blob).Value = postbyte;
com1.ExecuteNonQuery();
最后問題得以解決. 最后光這張表的容量從548.69G下降到了102.13G, 而且是在末改變其大小的情況下.
因為圖片中有些說明,劃線等,不可改變大小的. 如改變大小,可能更使容量變得更小.
代碼下載:https://files.cnblogs.com/lxy0423/pt.rar
posted on 2009-04-27 16:35 Jimmyzhang 阅读(1547) 评论(5) 编辑 收藏 举报