using System.Windows.Media.Imaging;
I try to convert BitmapImage or BitmapSource to Image,
BitmapImage.CopyPixels() throw an IOException "Can not read data from
stream".
what's wrong?
code like this:
------
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.UriSource = new Uri(fileName,
UriKind.RelativeOrAbsolute);
bitmapImage.EndInit();
//FileStream fs = new FileStream(this.strPhotoPath,
FileMode.Open, FileAccess.Read);
//GifBitmapDecoder decoder = new GifBitmapDecoder(new
Uri(this.strPhotoPath), BitmapCreateOptions.PreservePixelFormat,
BitmapCacheOption.OnDemand);
//BitmapSource bitmapImage = decoder.Frames[0];
int width = bitmapImage.PixelWidth;
int height = bitmapImage.PixelHeight;
int stride = width * ((bitmapImage.Format.BitsPerPixel + 7) /
8);
byte[] bits = new byte[height * stride];
//throw an IOException here:"Can not read data from stream"
bitmapImage.CopyPixels(bits, stride, 0);
//fs.Close();
unsafe
{
fixed (byte* pBits = bits)
{
IntPtr ptr = new IntPtr(pBits);
System.Drawing.Bitmap bitmap = new
System.Drawing.Bitmap(width, height, stride, PixelFormat.Format32bppPArgb,
ptr);
return bitmap;
}
}
-------