2012-01-16 19 views
7

मैं डेटाबेस में छवियों को संग्रहीत कर रहा हूं और उन्हें बाइट सरणी से छवि में परिवर्तित करना चाहता हूं। मुझे किसी ऑब्जेक्ट को बाइट सरणी में कनवर्ट करने में कोई समस्या नहीं है, लेकिन बाइट सरणी से छवि में कनवर्ट करने का प्रयास करते समय मुझे "पैरामीटर वैध नहीं है" की त्रुटि मिलती है। जिस वस्तु को मैं अपनी विधि में भेज रहा हूं वह डेटासेट पंक्ति से है।एसक्यूएल सर्वर पर एक छवि फ़ाइल सहेजना और बाइट सरणी को छवि में परिवर्तित करना

संग्रहित प्रक्रिया

USE [----------------] 
GO 
/****** Object: StoredProcedure [dbo].[usp_imageloader_add_test] Script Date: 01/16/2012 09:19:46 ******/ 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
ALTER procedure [dbo].[usp_imageloader_add_test] 
@p_Image Image 
as 

INSERT into Test_Images VALUES(@p_Image) 

अपलोड फ़ाइल नियंत्रण/बाइट सरणी के लिए छवि फ़ाइल बदलने और डेटाबेस के लिए डेटा को बचाने के जो कॉल objToImg विधि

protected void btnUpload_Click(object sender, EventArgs e) 
    { 
     if (ctrlUpload.PostedFile != null) 
     { 
      if (ctrlUpload.PostedFile.ContentLength > 0) 
      { 
       // Get Posted File 
       HttpPostedFile objHttpPostedFile = ctrlUpload.PostedFile; 

       // Find its length and convert it to byte array 
       int ContentLength = objHttpPostedFile.ContentLength; 

       // Create Byte Array 
       byte[] bytImg = new byte[ContentLength]; 

       // Read Uploaded file in Byte Array 
       objHttpPostedFile.InputStream.Read(bytImg, 0, ContentLength); 

       using (SqlConnection dbConnection = new SqlConnection(app_settings.sql_conn_string_db)) 
       { 
        try 
        { 
         string sql = "usp_imageloader_add_test"; 
         SqlCommand cmd = new SqlCommand(sql, dbConnection); 
         cmd.CommandType = System.Data.CommandType.StoredProcedure; 
         cmd.Parameters.AddWithValue("@p_Image", bytImg).SqlDbType = SqlDbType.Binary; 
         cmd.Connection.Open(); 
         cmd.ExecuteNonQuery(); 
         cmd.Connection.Close(); 
        } 


        catch (Exception ex) 
        { 
         ex.Message.ToString(); 
        } 
       } 
      } 
     } 
    } 

तालिका विधि

protected void Page_Load(object sender, EventArgs e) { generateTable(false); } private Table generateTable(bool flag) { Table tb = BuildList(GetData(), flag); if (imgloadercms != null) { PlaceHolder ph = new PlaceHolder(); StringBuilder sb = new StringBuilder(); ph.Controls.Add(new LiteralControl(sb.ToString())); } imgloadercms.Controls.Add(tb); return tb; } protected Table BuildList(DataTable tb, bool flag) { Table tblImageLibrary = new Table(); tblImageLibrary.BorderStyle = BorderStyle.Solid; tblImageLibrary.BorderWidth = Unit.Pixel(8); if (tb.Rows.Count > 0) { try { if (!flag) { tblImageLibrary.BorderColor = Color.Black; tblImageLibrary.BorderWidth = Unit.Pixel(1); TableRow tr = new TableRow(); // Table row for header of table tr.BackColor = Color.LightBlue; TableCell c1 = new TableCell(); TableCell c2 = new TableCell(); c1.Controls.Add(new LiteralControl("Image Id")); tr.Cells.Add(c1); c2.Controls.Add(new LiteralControl("Image")); tr.Cells.Add(c2); tblImageLibrary.Rows.Add(tr); } int i = 0; foreach (DataRow r in tb.Rows) // Create new row foreach row in table { TableRow tr = new TableRow(); if (i % 2 == 0) { tr.BackColor = Color.LightYellow; } // Build cells TableCell c1 = new TableCell(); TableCell c2 = new TableCell(); c2.Width = 300; c1.Controls.Add(new LiteralControl(r["Image_Id"].ToString())); tr.Cells.Add(c1); // Call method to serialize obj to byte array //System.Drawing.Image dbImg = ObjToImg(r["Image_File"]); } catch (Exception ex) { ex.ToString(); } if (!flag) { } } return tblImageLibrary; } 

वापसी छवि

private System.Drawing.Image ObjToImg(object obj) 
    { 
     //byte[] byteArray = null; 

     if (obj == null) 
      return null; 
     else 
     { 

      BinaryFormatter bf = new BinaryFormatter(); 
      using (MemoryStream ms = new MemoryStream()) 
      { 
       bf.Serialize(ms, obj); //now in Memory Stream 
       ms.ToArray(); // Array object 
       ms.Seek(0, SeekOrigin.Begin); 

       //return (System.Drawing.Image)bf.Deserialize(ms); 

       System.Drawing.Image myImage = (System.Drawing.Image)bf.Deserialize(ms); 

       return myImage; 
      } 

जब भी मैं छवि वस्तु निर्माता मैं की त्रुटि संदेश प्राप्त करने के लिए स्मृति धारा वस्तु को जोड़ने का प्रयास "पैरामीटर मान्य नहीं है"। हो सकता है कि मैंने डेटाबेस में बाइट सरणी डालने पर गलती की क्योंकि मैंने दूसरे कोड को देखा है और यह समझ में नहीं आता कि यह कैसे काम नहीं कर रहा है।

+0

हमें यह भी दिखाएं कि आप बाइट्स को 'ObjToImg' –

+0

पर पास करते हैं, मेरे पास समान कोड था, जिसमें मैंने 'छवि' के बजाय' varbinary (max)' का उपयोग किया था। संपादित करें: बस यह लिंक भी मिला - http://stackoverflow.com/questions/4113294/is-there-a-big-technical-difference-between-varbinarymax-and-image-data-types – robasta

+0

इसकी एक पंक्ति से डेटा टेबल मैं अपनी मूल पोस्ट अपडेट करूंगा। –

उत्तर

1

को अपने बाइनरीफॉर्मेटर के साथ पहले बाइट सरणी से ऑब्जेरियल करें।

दो तरीकों का पालन उपयोग करने के लिए प्रयास करें:

private System.Drawing.Image ObjToImg(byte[] obj) 
    { 
     if (obj == null) 
      return null; 
     else 
     { 
      BinaryFormatter bf = new BinaryFormatter(); 
      using(MemoryStream ms = new MemoryStream(obj)) 
      { 
       return (System.Drawing.Image)bf.Deserialize(ms); 
      } 
     } 
    } 
private byte[] ImgToObj(System.Drawing.Image obj) 
    { 
     if (obj == null) 
      return null; 
     else 
     { 
      BinaryFormatter bf = new BinaryFormatter(); 
      using(MemoryStream ms = new MemoryStream()) 
      { 
       bf.Serialize(ms, obj); 
       return ms.ToArray(); 
      } 
     } 
    } 
+0

एसकएलडीबी टाइप को मूल रूप से छवि पर सेट किया गया था, लेकिन मैंने कोई फर्क नहीं पड़ता। –

+0

मैंने कोशिश की और इसने "एक खाली धारा को deserialize करने की कोशिश" का अपवाद फेंक दिया। –

+0

आप निश्चित रूप से अपने फॉर्मेटर के आउटपुट के साथ ऑपरेशन के साथ एक समस्या को हल करते हैं - इसे जांचें। –

1

मैं अभी हाल ही में VB.NET में सटीक एक ही बात करना था। टेलरिक कोड कन्वर्टर के माध्यम से चलाए जाने वाला मेरा कोड यहां है। काम करना मुश्किल था, और अभी भी ईमानदारी से दर्द है।

एक छवि अपलोड करने के लिए:

private bool uploadImage(ref Bitmap p) 
{ 
    SqlConnection con = new SqlConnection(); 
    con.ConnectionString = Configuration.ConfigurationManager.ConnectionStrings("ConnStringHere").ConnectionString; 
    SqlCommand cmd = new SqlCommand(); 
    cmd.CommandText = "INSERT INTO Table_Name (File2) VALUES (@File2)"; //I named the column File2 simply because "File" seemed to be a keyword in SQLServer 
    cmd.CommandType = CommandType.Text; 
    cmd.Connection = con; 

    SqlParameter File1 = new SqlParameter("@File2", SqlDbType.Image); 
    MemoryStream ms = new MemoryStream(); 

    using (Bitmap tempImage = new Bitmap(p)) 
    { 
     tempImage.Save(ms, p.RawFormat); 
    } 

    byte[] data = ms.GetBuffer(); 
    if (!isValidImage(data)) //optional, will include code if requested. 
    { 
     return false; 
    } 
    File1.Value = data; 
    cmd.Parameters.Add(File1); 

    con.Open(); 
    int result = cmd.ExecuteNonQuery(); 
    if (result > 0) 
    { 
     // SUCCESS! 
     con.Close(); 
     return true; 
    } 
    else 
    { 
     //failure 
     con.Close(); 
     return false; 
    } 

} 

एक छवि को पुनः प्राप्त करने के लिए:

private Bitmap retrieveBitmap() 
    { 
     Image image1 = null 
     if (dt1.Rows.Count > 0) 
     { 
      byte[] imageData1 = null; 
      if (dt1[0].Count > 0) 
      { 
       if (!Information.IsDBNull(dt1.CopyToDataTable()[0].Item("File2"))) 
       { 
        imageData1 = (byte[])dt1.CopyToDataTable()[0].Item("File2"); 
       } 
      } 
      if ((imageData1 != null)) 
      { 
       if (isValidImage(imageData1)) 
       { 
        using (MemoryStream ms = new MemoryStream(imageData1, 0, imageData1.Length)) 
        { 
         ms.Write(imageData1, 0, imageData1.Length); 
         image1 = Image.FromStream(ms, true); 
        } 
        return image1; 
       } 
       else 
       { 
        // "Invalid image on server"; 
        return null; 
       } 
      } 
     } 
    } 

मेरे कोड छोटे स्वरूपण परिवर्तन की आवश्यकता हो सकती, संपादित करें जो कुछ भी अमान्य वाक्यविन्यास है (मेरी सी # कुछ पुरानी है, और मेरा कोड कनवर्टर के माध्यम से चलाया गया था)।

संबंधित मुद्दे