I have a method to resize the image like below.
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;
}
I am using WritableBitmapEx library and after resizing my image I convert it to a byte array using code below in client side:
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;
}
Then I am writing bytes to file in server side using BinaryWriter
. However I cannot see the image. When I open the file, it says file is currupted. If I send picture array without resizing there is no problem. Is there a bug with WriteableBitmapEx library or something wrong with my code?