0

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?

Blast
  • 955
  • 1
  • 17
  • 40
  • 3
    What is `ToByteArray()`? There's no such method in the `WriteableBitmap` class itself, so I suspect an extension method in the library you're using. Most likely it only returns the bytes that are the actual bitmap data, rather than a valid bitmap file format. You can use that if you like, but you will need to deserialize correctly as well; presumably if your library has a `ToByteArray()` extension method, it also has a `FromByteArray()` (or similar) factory method somewhere. So use that instead of trying to interpret the data as a file. Alternatively, use a proper encoder to write the file. – Peter Duniho Oct 15 '15 at 16:37
  • Unfortunately, without [a good, _minimal_, _complete_ code example](https://stackoverflow.com/help/mcve) that reliably reproduces the problem, it's not possible to know for certain what the actual problem is, never mind confidently provide a useful answer. – Peter Duniho Oct 15 '15 at 16:38

0 Answers0