2013-05-28 81 views
1

嗨,大家我有我的代碼有問題,我保存爲varbinary圖像確定,但我希望圖像顯示在一個圖片框從fileupload中選擇時,但我一直得到一個錯誤,這可能嗎?我使用的是C#和asp.net。我一直在尋找,但我似乎無法讓我的源代碼工作。檢索varbinary數據c#圖片框

using System; 
using System.Data; 
using System.Data.SqlClient; 
using System.Linq; 
using System.Web.UI.WebControls; 
using System.IO; 
using System.Drawing; 

public partial class Mensagem : System.Web.UI.Page 
{   
    protected void BtnUploadClick(object sender, EventArgs e) 
    { 
     string filePath = Server.MapPath(upload1.FileName); 
     string filename = Path.GetFileName(filePath); 
     string ext = Path.GetExtension(upload1.PostedFile.FileName); 
     Stream fs = upload1.PostedFile.InputStream; 
     BinaryReader br = new BinaryReader(fs); 
     Byte[] bytes = br.ReadBytes((Int32)fs.Length); 
     string dt = DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss");  

     SqlConnection conn = new SqlConnection("Data Source=Microsoft SQL Server (SqlClient);Server=NEVETS-LAPTOP\\NEVETS;Initial Catalog=Forum;uid=sa;pwd=sql;Connect Timeout=10;TrustServerCertificate=True "); 
       conn.Open(); 

      SqlCommand sqlCommand = conn.CreateCommand(); 
      sqlCommand.CommandType = CommandType.StoredProcedure; 
      sqlCommand.CommandText = "usp_inserirFicheiro";     
      sqlCommand.Parameters.AddWithValue("@nome", SqlDbType.VarChar).Value = filename; 
      sqlCommand.Parameters.AddWithValue("@tamanho", SqlDbType.VarBinary).Value = bytes; 
      sqlCommand.Parameters.AddWithValue("@formato", SqlDbType.VarChar).Value = ext; 
      sqlCommand.Parameters.AddWithValue("@data", SqlDbType.DateTime).Value = dt; 
      sqlCommand.Parameters.AddWithValue("@ficheiro", SqlDbType.VarBinary).Value = br;        
      sqlCommand.ExecuteNonQuery(); 
      txtnome.Text = "sucesso"; 

      conn.Close(); 

    } 


    protected void Button2_Click(object sender, EventArgs e) 
    { 
     //upload1.SaveAs(Server.MapPath("~/imgs/" + upload1.FileName).ToString()); 
     //pbficheiro.ImageUrl = @"~/imgs/" + upload1.FileName; 


Byte[] ba = new BinaryReader(upload1.PostedFile.InputStream).ReadBytes((Int32)upload1.PostedFile.InputStream.Length); 
using (MemoryStream ms = new MemoryStream(ba, false)) 
{ 
    Image imgTmp = Image.FromStream(ms); 
    Bitmap bm = new Bitmap(imgTmp.Width, imgTmp.Height); 
    Graphics g = Graphics.FromImage(bm); 
    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 
    g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; 
    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; 
    Rectangle rect = new Rectangle(0, 0, imgTmp.Width, imgTmp.Height); 
    g.DrawImage(imgTmp, rect, 0, 0, imgTmp.Width, imgTmp.Height, GraphicsUnit.Pixel); 

    using (MemoryStream ms2 = new MemoryStream()) 
    { 
     bm.Save(ms2, imgTmp.RawFormat); 
      bm.Dispose(); 
      imgTmp.Dispose(); 
      if (WantAnImage) 
       return Image.FromStream(ms2); 
      else (WantBytes) 
       ms2.ToArray();} 
    } 
} 

} 
+0

pbficheiro是我的PictureBox的,不應該也呈現出加載圖像從文件上傳? – user2427935

回答

0
因爲

的圖像數據被設置成與流對象時此方法退出如果錯誤存儲器相關的,則很可能。嘗試將其捕獲到中間位圖對象中,並將該位圖分配給PictureBox圖像屬性。

這裏有一個鏈接,可以幫助:

http://msdn.microsoft.com/en-us/library/z7ha67kw

編輯:

我不

Saving Image to MemoryStream- Generic GDI+ Error

歸根結底,這是作爲一個信息源引用t知道什麼類型的「upload1」是或其範圍,但是荷蘭國際集團是本地的對象,我想知道這個方法(這將是你會打電話來檢索字節]或圖像的方法):

protected void Button2_Click(object sender, EventArgs e) 
{ 
    //upload1.SaveAs(Server.MapPath("~/imgs/" + upload1.FileName).ToString()); 
    //pbficheiro.ImageUrl = @"~/imgs/" + upload1.FileName; 

    Byte[] ba = new BinaryReader(upload1.PostedFile.InputStream).ReadBytes((Int32)upload1.PostedFile.InputStream.Length); 
    using (MemoryStream ms = new MemoryStream(ba, false)) 
    { 
     System.Drawing.Image imgTmp = System.Drawing.Image.FromStream(ms); 
     Bitmap bm = new Bitmap(imgTmp.Width, imgTmp.Height); 
     Graphics g = Graphics.FromImage(bm); 
     g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 
     g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; 
     g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; 
     Rectangle rect = new Rectangle(0, 0, imgTmp.Width, imgTmp.Height); 
     g.DrawImage(imgTmp, rect, 0, 0, imgTmp.Width, imgTmp.Height, GraphicsUnit.Pixel); 
       bm.Save(Server.MapPath("~/imgs/" + upload1.FileName).ToString()); 
       pbficheiro.ImageUrl = @"~/imgs/" + upload1.FileName; 
    } 
} 
+0

感謝您的答案,即時嘗試要做的是這 protected void Button2_Click(object sender,EventArgs e) { upload1.SaveAs(Server.MapPath(「〜/ imgs /」+ upload1.FileName).ToString( )); pbficheiro.ImageUrl = @「〜/ imgs /」+ upload1.FileName; 但是不需要在本地保存文件...我希望你能理解... – user2427935

+0

這與你原來的發佈代碼有點不同。但是第一次嘗試並不壞,我只是擔心記憶丟失了。它不需要本地保存(我的建議也沒有)。您只需將該內存交給不會立即處理的內容,例如將其傳遞給本地Bitmap對象或某些不依賴MemoryStream的類似構造。 – DonBoitnott

+0

感謝您的快速回答,我一直試圖從記憶中得到它,就像你說的,但我不認爲我得到它的權利......我不斷在我的代碼上得到錯誤。不能隱含轉換類型...任何建議? – user2427935