خطأ في بايت [] لتحويل WPF Bitmapimage؟
-
28-09-2019 - |
سؤال
أنا أنقذ نقطية إلى بايت [] لتوفير DB. أنا متأكد من أن البيانات يتم حفظها واستردادها بدقة حتى لا تكون مشكلة هناك.
على بايت [] إلى تحويل نقطية ، استمر في الحصول على استثناء من "System.NotSupportedException: لم يتم العثور على مكون تصوير مناسب لإكمال هذه العملية."
هل يمكن لأي شخص أن يرى ما أفعله خطأ في وظيفتي هنا؟
private Byte[] convertBitmapImageToBytestream(BitmapImage bi)
{
int height = bi.PixelHeight;
int width = bi.PixelWidth;
int stride = width * ((bi.Format.BitsPerPixel + 7) / 8);
Byte[] bits = new Byte[height * stride];
bi.CopyPixels(bits, stride, 0);
return bits;
}
public BitmapImage convertByteToBitmapImage(Byte[] bytes)
{
MemoryStream stream = new MemoryStream(bytes);
stream.Position = 0;
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = stream;
bi.EndInit();
return bi;
}
المحلول 3
تبين أن Copypixels النقطية ليست صحيحة. أنا آخذ إخراج نقطات النقطات وتحويله إلى شيء قابل للاستخدام في هذه الحالة JPG.
public static Byte[] convertBitmapImageToBytestream(BitmapImage bi)
{
MemoryStream memStream = new MemoryStream();
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bi));
encoder.Save(memStream);
byte[] bytestream = memStream.GetBuffer();
return bytestream;
}
نصائح أخرى
هل يساعد هذا السؤال Stackoverflow؟
بايت [] إلى نقطية في Silverlight
تعديل:
جرب هذا ، لست متأكدًا من أنه سيعمل:
public BitmapImage convertByteToBitmapImage(Byte[] bytes)
{
MemoryStream stream = new MemoryStream(bytes);
stream.Position = 0;
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.CacheOption = BitmapCacheOption.OnLoad;
bi.DecodePixelWidth = ??; // Width of the image
bi.StreamSource = stream;
bi.EndInit();
return bi;
}
تحديث 2:
لقد وجدت هذه:
قم بتحميل بايت [] في صورة في وقت التشغيل
نقطية من البايت [] على غير uithread
بصرف النظر عن ذلك ، لا أعرف.
كيف تعرف أن تنسيق البايت [] الذي تقوم بإنشائه هو ما تتوقعه BI في الدفق؟ لماذا لا تستخدم bitmapimage.streamsource لإنشاء البايت [] الذي تحفظه؟ ثم تعرف أن التنسيق سيكون متوافقًا.
http://www.codeproject.com/kb/vb/bmpimage2bytearray.aspx
http://social.msdn.microsoft.com/forums/en-us/wpf/thread/8327dd31-2db1-4daa-a81c-aff60b63fee6/
لم أجرب أيًا من هذا الرمز ، لكن يمكنك