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我将字节存储在datatable中它存储一个值System.Byte []而不是实际的字节..当我得到datatable中的所有值并将其放入查询“插入表值('System.Byte []')。it存储字符串System.Byte“不是二进制数据..
答案 0 :(得分:2)
您已将所有数据加载到MemoryStream
并重新输入流 - 一切正常(尽管new MemoryStream(byteArrayIn)
会更容易)。
这留下了一个简单的可能性:数组真的不包含它应该拥有的所有数据。
检查你是如何获得阵列的,以及所有中间步骤。特别是,添加一些调试代码以记录数组时的长度,并检查是否已完成所有操作。如果长度相同,请检查内容是否是逐字节相同的(比较Convert.ToBase64String
的输出可能是进行临时使用时最方便的方法)
如果您正在编写流,请检查以下几点:
Stream.Read
/ Stream.Write
循环,请检查您是否正确使用了Read
的返回值;或者更简单 - 只需使用Stream.CopyTo
代替(这有正确实施的Read
/ Write
循环)MemoryStream
,请务必了解ToArray
和GetBuffer
之间的区别,以及每个StreamReader
/ StreamWriter
/ Encoding
/ GetString
/类似