سؤال

وحاليا أعمل على مشروع المسح بالموجات فوق الصوتية، والذي يعرض على تواصل الصور المكتسبة من التحقيق، أن تفعل ذلك أنا أكتب البرمجية التالية.

وXAML:

<Image Name="imgScan" DataContext="{Binding}" Source="{Binding Path=prescanImage,Converter={StaticResource imgConverter}}" />

وC # تعيين:

Bitmap myImage = GetMeImage();
imageMem = new MemoryStream();
myImage .Save(imageMem, ImageFormat.Png);
imgScan.DataContext = new { prescanImage = imageMem.ToArray() };

ومحول:

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    if (value != null && value is byte[])
    {
      byte[] ByteArray = value as byte[];
      BitmapImage bmp = new BitmapImage();
      bmp.BeginInit();
      bmp.StreamSource = new MemoryStream(ByteArray);
      bmp.EndInit();
      return bmp;
    }
    return null;
}

وهذه الطريقة تكلف لي الكثير من (الأداء)، هل هناك أي طريقة أفضل للقيام بذلك ؟؟

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

المحلول

ومنذ كنت تقوم بالفعل DataContext في التعليمات البرمجية (لا XAML)، لماذا لا مجرد تخطي خطوات قليلة؟

Bitmap myImage = GetMeImage();
imageMem = new MemoryStream();
myImage.Save(imageMem, ImageFormat.Png);
BitmapImage bmp = new BitmapImage();
bmp.BeginInit();
bmp.StreamSource = new MemoryStream(imageMem.ToArray());
bmp.EndInit();
imgScan.Source = bmp;

إذا كان لديك الوصول إلى GetMeImage()، قد ترغب في النظر في تغيير لتناسب بشكل أفضل في التطبيق الخاص بك - هل حقا بحاجة للعودة إلى Bitmap

وأيضا، كيف وغالبا ما أول قطعة من التعليمات البرمجية يتم تنفيذه؟ قد ترغب في النظر في تغيير ذلك، أو السماح لها أن تختلف عندما يحتاج إليها.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top