Note that there are some explanatory texts on larger screens.

plurals
  1. POContinuously Updating Silverlight Image control from Byte[] in Backgroundworker (about every 100 ms)
    primarykey
    data
    text
    <p>I am new to posting in forums, as I usually can help my self through searches, but am really stuck here...</p> <p>I have written an ASP.NET C# WebApp, which also needs to incorporate a client side serial com interface to a device that basically does live scanning, and streams the images scanned through the serial (USB) interface (These images need to be refreshed Continuously, approximately every 100ms, therefore basically creating an "animation" effect)</p> <p>To get this to run Client side, I have written a small Silverlight App. running In Browser, with the following code, using a Backgroundworker trying to separate Serial Comms from UI, and refreshing the Silverlight image control from the Byte Arrays received.</p> <p>I have VERY SIMILAR code working 100% in WinForms, my only issue in Silverlight, is that no image ever gets displayed in my image control.</p> <p>Below is the relevant WinForms code followed by the corresponding Silverlight code.</p> <p>My current suspicion is that the image is never rendered as Silverlight only allows for PixelFormat of 32bppArgb where I need to use PixelFormat.Format8bppIndexed, as per my WinForms CreateBitmap() method below.</p> <p>If this is indeed the issue, I cannot find any way to create this format of Bitmap in Silverlight.</p> <pre><code>/////////////// WINFORMS CODE (Timer1 Interval = 100ms) ////////////////// private void timer1_Tick(object sender, EventArgs e) { BackgroundWorker bw = new BackgroundWorker(); bw.WorkerReportsProgress = true; bw.DoWork += new DoWorkEventHandler( delegate(object o, DoWorkEventArgs args) { BackgroundWorker b = o as BackgroundWorker; int BytesToRead = COMport.Read(ReceiveBuffer); for (int i = 0; i &lt; BytesToRead; i++) { //Code that copies ReceiveBuffer to byte[] LiveImgArr Bitmap liveBMP = CreateBitmap(LiveImgArr, imgWidth, imgHeight); bw.ReportProgress(i, liveBMP); } }); bw.ProgressChanged += new ProgressChangedEventHandler( delegate(object o, ProgressChangedEventArgs args) { pictureBox1.Image = (Bitmap)args.UserState; }); bw.RunWorkerAsync(); } private Bitmap CreateBitmap(byte[] buffer, int width, int height) { Bitmap bmp = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format8bppIndexed); BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed); System.Runtime.InteropServices.Marshal.Copy(buffer, 0, bmpData.Scan0, width * height); bmp.UnlockBits(bmpData); ColorPalette pal = bmp.Palette; for (int i = 0; i &lt; 256; i++) { pal.Entries[i] = Color.FromArgb(i, i, i); } bmp.Palette = pal; return bmp; } //////////////////////////////////////// END ///////////////////////////////////////// //////////////////////////////// SilverLight Code /////////////////////////////////// public void StartTimer()//object o, RoutedEventArgs sender) { System.Windows.Threading.DispatcherTimer CommsTimer = new System.Windows.Threading.DispatcherTimer(); CommsTimer.Interval = new TimeSpan(0, 0, 0, 0, 100); // 100 Milliseconds CommsTimer.Tick += new EventHandler(CommsTimer_Tick); CommsTimer.Start(); } public void CommsTimer_Tick(object o, EventArgs sender) { BackgroundWorker bw = new BackgroundWorker(); bw.WorkerReportsProgress = true; bw.DoWork += new DoWorkEventHandler( delegate(object b, DoWorkEventArgs args) { BackgroundWorker obw = b as BackgroundWorker; int BytesToRead = COMport.Read(ReceiveBuffer); for (int i = 0; i &lt; BytesToRead; i++) { //Code that copies ReceiveBuffer to byte[] LiveImgArr bw.ReportProgress(i, LiveImgArr); } } }); bw.ProgressChanged += new ProgressChangedEventHandler( delegate(object j, ProgressChangedEventArgs args) { byte[] imgByte = (byte[])args.UserState; using (MemoryStream ms = new MemoryStream(imgByte, 0, imgByte.Length)) { BitmapImage bmp = new BitmapImage(); bmp.SetSource(ms); this.image1.Source = bmp; } }); //////////////////////////////////////// END ////////////////////////////////////////// ///////////////////////////////// NEW CODE AS PER CLEMENTS///////////////////////////// bw.ProgressChanged += new ProgressChangedEventHandler( delegate(object j, ProgressChangedEventArgs args) { byte[] imgByte = (byte[])args.UserState; WriteableBitmap wbmp = new WriteableBitmap(208, 208); int[] wbmpArray = wbmp.Pixels; for (int pixelIndex = 0; pixelIndex &lt; imgByte.Length; pixelIndex++) { byte alpha = 128; byte red = 255; byte green = 255; byte blue = 255; double scaleAlpha = alpha / 255.0; // we are not using scaleAlpha here //wbmp.Pixels[pixelIndex] = // (alpha &lt;&lt; 24) // | (red &lt;&lt; 16) // | (green &lt;&lt; 8) // | blue; // notice the alpha value is NOT scaled // it’s also very important to scale BEFORE // shifting the values wbmp.Pixels[pixelIndex] = (alpha &lt;&lt; 24) | ((byte)(red * scaleAlpha) &lt;&lt; 16) | ((byte)(green * scaleAlpha) &lt;&lt; 8) | (byte)(blue * scaleAlpha); } wbmp.Invalidate(); this.image1.Source = wbmp; //ImageBrush imgBrush = new ImageBrush(); //imgBrush.ImageSource = wbmp; //imgRect.Fill = imgBrush; }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload