2012-10-30 70 views
1
public static Image Crop(Image imgPhoto, int Width, int Height, AnchorPosition Anchor) 
     { 
      if (imgPhoto == null) 
      { 
       return null; 
      } 
      int sourceWidth = imgPhoto.Width; 
      int sourceHeight = imgPhoto.Height; 
      int sourceX = 0; 
      int sourceY = 0; 
      int destX = 0; 
      int destY = 0; 

      float nPercent = 0; 
      float nPercentW = 0; 
      float nPercentH = 0; 

      nPercentW = ((float)Width/(float)sourceWidth); 
      nPercentH = ((float)Height/(float)sourceHeight); 

      if (nPercentH < nPercentW) 
      { 
       nPercent = nPercentW; 
       switch (Anchor) 
       { 
        case AnchorPosition.Top: 
         destY = 0; 
         break; 
        case AnchorPosition.Bottom: 
         destY = (int)(Height - (sourceHeight * nPercent)); 
         break; 
        default: 
         destY = (int)((Height - (sourceHeight * nPercent))/2); 
         break; 
       } 
      } 
      else 
      { 
       nPercent = nPercentH; 
       switch (Anchor) 
       { 
        case AnchorPosition.Left: 
         destX = 0; 
         break; 
        case AnchorPosition.Right: 
         destX = (int)(Width - (sourceWidth * nPercent)); 
         break; 
        default: 
         destX = (int)((Width - (sourceWidth * nPercent))/2); 
         break; 
       } 
      } 

      int destWidth = (int)(sourceWidth * nPercent); 
      int destHeight = (int)(sourceHeight * nPercent); 

      Bitmap bmPhoto = new Bitmap(Width, Height, PixelFormat.Format24bppRgb); 
      bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution); 

      Graphics grPhoto = Graphics.FromImage(bmPhoto); 
      grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic; 

      grPhoto.DrawImage(imgPhoto, 
       new Rectangle(destX, destY, destWidth, destHeight), 
       new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), 
       GraphicsUnit.Pixel); 

      grPhoto.Dispose(); 
      return bmPhoto; 
     } 


    public byte[] ImageToByteArray(string path) 
     { 
      FileInfo info = new FileInfo(path); 
      long sizeByte = info.Length; 

      FileStream fs = new FileStream(path,FileMode.Open,FileAccess.Read); 

      BinaryReader br = new BinaryReader(fs); 

      byte[] data = br.ReadBytes((int) sizeByte); 
      return data; 
     } 


     public byte[] ImageToByteArray(Image img) 
     { 

      if (img == null) 
       return null; 

      BinaryFormatter bf = new BinaryFormatter(); 
      MemoryStream ms = new MemoryStream(); 
      bf.Serialize(ms, img); 
      return ms.ToArray(); 

     } 

    public Image BrowseImage(Image image) 
     { 
      OpenFileDialog open = new OpenFileDialog(); 
      open.FileName = string.Empty; 

      open.Filter = "Image Files(*.png; *.jpg; *.jpeg; *.gif; *.bmp)|*.png; *.jpg; *.jpeg; *.gif; *.bmp"; 
      if (open.ShowDialog() == DialogResult.OK) 
      { 
       Image img = new Bitmap(open.FileName); 
       if ((img.Width < 200) || (img.Height < 200)) 
       { 
        MessageBox.Show("Minimum size is 200x200."); 
        BrowseImage(image); 
       } 
       else 
       { 
        return img; 
       } 
      } 
      return image; 
     } 


in saving image 

    picItem.Image = Crop(BrowseImage(picItem.Image), 200, 200, ImageUtil.AnchorPosition.Center); 



//set Datatable row 
    erow["Image"] = img.ImageToByteArray(picItem.Image); 


//Saving is ok 

    //When i View 
    picItem.Image = ByteArrayToImage((byte[])source.Image.binaryFromDB); 


>Error: End of Stream encountered before parsing was completed? 

這是所有使用的方法。我裁剪圖像,以便它有一個最小尺寸..嘗試任何轉換,但沒有幫助。 我只想保存圖像到數據庫,當我查看我可以看到圖片框中的圖像。數據流解析錯誤結束

@MarcGravell我發現了這個問題。 ü正確的man.when我存儲在數據表中的字節它存儲一個值System.Byte []不是實際的字節..當我得到所有的數據表中的值,並把它放在一個查詢「Insert into table values('System.Byte []').it存儲字符串System.Byte「不是二進制數據。

+2

什麼是'byteArrayIn'?它是如何填充的? –

+0

你確定'byteArrayIn'包含所有的數據嗎? – Rafal

+0

byteArrayIn是字節數組? –

回答

2

您已經將所有數據加載到MemoryStream中,並且重新引導該流 - 所有的都是好的(儘管new MemoryStream(byteArrayIn)會更容易)。

這留下了一個簡單的可能性:數組確實沒有包含它應該擁有的所有數據。

檢查您是如何得到數組以及所有中間步驟的。特別是,當你編寫時,添加一些調試代碼來記錄數組的長度,並檢查你是否全部使用它。如果長度相同,請檢查內容是否是相同的逐字節(比較Convert.ToBase64String的輸出可能是用於臨時使用的最方便的方式)。

如果你正在寫流,查了幾件事情:

  • 如果使用的是Stream.Read/Stream.Write循環,檢查正在使用從Read正確的返回值;或者更簡單 - 只需使用Stream.CopyTo,而不是(這有一個正確實現Read/Write環)
  • 如果使用MemoryStream,請確保您瞭解ToArrayGetBuffer,如果你正在傳送的每個
  • 的影響之間的差異數據在任何時候,確保你在任何時候都把它視爲作爲二進制 - 從來沒有文字。這樣的無StreamReader/StreamWriter/Encoding/GetString /什麼
+0

我用它來轉換二進制圖像。 'BinaryFormatter bf = new BinaryFormatter(); MemoryStream ms = new MemoryStream(); bf.Serialize(ms,img); return ms.ToArray();' – Snippet

+0

@Snippet正確;那個'ms.ToArray()'和這個反序列化代碼之間的數組會發生什麼?它會轉到一個文件嗎?到數據庫?任何其他中間步驟?或者直接從序列化到反序列化? –

+0

序列化後我將字節存儲在數據表中,然後數據表中的數據保存在數據庫中。 – Snippet

相關問題