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?

this is all the method ive use. i crop the image so that it will have a minimum size..ive tried any convertion but no help. i just want to save an image to database and when i view i can see the image in picture box.

@MarcGravell I found the problem. u right man.when i store the byte in datatable it stores a value System.Byte[] not the actual bytes.. when i get all the value in datatable and put it in a query "Insert into table values('System.Byte[]'). .it stores a string System.Byte" not the binary data..

4

1 回答 1

2

You've loaded all the data into the MemoryStream and rewound the stream - all good (although new MemoryStream(byteArrayIn) would have been easier).

This leaves one simple possibility: the array truly does not contain all of the data that it should have.

Check how you got the array, and all intermediary steps. In particular, add some debug code to note the length of the array when you write it, and check that you got all of it. If the length is the same, check that the contents are identical byte-for-byte (comparing the output of Convert.ToBase64String is probably the most convenient way of doing that for ad-hoc usage).

If you are writing streams, check a few things:

  • if using a Stream.Read/Stream.Write loop, check you are using the return value from Read correctly; or easier - just use Stream.CopyTo instead (this has a correctly-implemented Read/Write loop)
  • if using MemoryStream, make sure you understand the difference between ToArray and GetBuffer, and the implications of each
  • if you are transferring the data at any point, make sure you are treating it as binary at all times - never text. No StreamReader / StreamWriter / Encoding / GetString / anything like that
于 2012-10-30T07:52:35.767 回答