سؤال

لقد كنت أحاول تسلسل وتهرب من نقطات. لقد كنت أستخدم الطرق التي من المفترض أن تعمل في هذا الموضوع: خطأ في بايت [] لتحويل WPF Bitmapimage؟

فقط لتكرار ما يجري ، إليك جزء من رمز التسلسل الخاص بي:

using (MemoryStream ms = new MemoryStream())
                {
                    // This is a BitmapImage fetched from a dictionary.
                    BitmapImage image = kvp.Value; 

                    PngBitmapEncoder encoder = new PngBitmapEncoder();
                    encoder.Frames.Add(BitmapFrame.Create(image));
                    encoder.Save(ms);

                    byte[] buffer = ms.GetBuffer();

                    // Here I'm adding the byte[] array to SerializationInfo
                    info.AddValue((int)kvp.Key + "", buffer);
                }

وهنا هو رمز التسلل:

// While iterating over SerializationInfo in the deserialization
// constructor I pull the byte[] array out of an 
// SerializationEntry
using (MemoryStream ms = new MemoryStream(entry.Value as byte[]))
                    {
                        ms.Position = 0;

                        BitmapImage image = new BitmapImage();
                        image.BeginInit();
                        image.StreamSource = ms;
                        image.EndInit();

                        // Adding the timeframe-key and image back into the dictionary
                        CapturedTrades.Add(timeframe, image);
                    }

أيضًا ، لست متأكدًا مما إذا كان الأمر مهمًا ولكن في وقت مبكر عندما قمت بالسكان في القاموس ، قمت بتشفير نقار صورة نقطية مع pngbitmapencoder لإدخالها في نقطية. لذلك لست متأكدًا مما إذا كان الترميز المزدوج له علاقة به. هذه هي الطريقة التي تفعل ذلك:

// Just to clarify this is done before the BitmapImages are added to the
// dictionary that they are stored in above.
private BitmapImage BitmapConverter(Bitmap image)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                BitmapImage bImg = new BitmapImage();
                bImg.BeginInit();
                bImg.StreamSource = new MemoryStream(ms.ToArray());
                bImg.EndInit();
                ms.Close();

                return bImg;
            }
        }

وبالتالي فإن المشكلة هي أن التسلسل والتسلسل يعمل بشكل جيد. لا توجد أخطاء ، ويحتوي القاموس على إدخالات مع ما يبدو أنه نقطات ، ومع ذلك عرض/ارتفاعه وبعض الخصائص الأخرى يتم تعيينها على "0" عندما أنظر إليها في وضع تصحيح الأخطاء. وبالطبع ، لا يتم عرض أي شيء عندما أحاول عرض الصور.

هل هناك أي أفكار حول سبب عدم تعويضهم بشكل صحيح؟

شكرًا!

هل كانت مفيدة؟

المحلول

1) يجب ألا تتخلص من MemoryStream ، وتستخدم من تهيئة الصورة. إزالة using في هذا الخط

using (MemoryStream ms = new MemoryStream(entry.Value as byte[]))

2) بعد

encoder.Save(ms);

حاول إضافة

ms.Seek(SeekOrigin.Begin, 0);
ms.ToArray();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top