我有一种调整图像大小的方法。
public static WriteableBitmap ResizedBitmap(BitmapImage imgPhoto, int Width, int Height)
{
int sourceWidth = (int)imgPhoto.PixelWidth;
int sourceHeight = (int)imgPhoto.PixelHeight;
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 = nPercentH;
destX = System.Convert.ToInt16((Width -
(sourceWidth * nPercent)) / 2);
}
else
{
nPercent = nPercentW;
destY = System.Convert.ToInt16((Height -
(sourceHeight * nPercent)) / 2);
}
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
WriteableBitmap bmPhoto = new WriteableBitmap(imgPhoto);
WriteableBitmap resizedBitmap = bmPhoto.Resize(destWidth, destHeight, WriteableBitmapExtensions.Interpolation.NearestNeighbor);
return resizedBitmap;
}
我正在使用WritableBitmapEx库,在调整图像大小后,我使用客户端下面的代码将其转换为字节数组:
using (FileStream stream = openFileDialog.File.OpenRead())
{
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(stream);
WriteableBitmap resizedWriteableBitmap = ImageHelper.ResizedBitmap(bitmapImage, 150, 200);
member_Image.Source = resizedWriteableBitmap;
memberImageByteArray = resizedWriteableBitmap.ToByteArray();
fileExtension = fileInfo.Extension;
}
然后我使用BinaryWriter
在服务器端将文件写入文件。但是我看不到图像。当我打开文件时,它说文件被破坏了。如果我发送图片阵列而不调整大小没有问题。 WriteableBitmapEx库是否有错误或我的代码有问题?