2012-10-30 65 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

相关问题