How to update WritableBitmap by byte[] in WPF

175 Views Asked by At

BitmapImage can auto get PixelHeight and PixelWidth and PixelFormat and so on by set an byte[] to StreamSource. But WritableBitmap could not. Must I convert byte[] to BitmapImage, Get PixelHeight and PixelWidth and PixelFormat, compare with existing WritableBitmap. if pixel changed, new a WritableBitmap to update it, if not changed, just BitmapImage.CopyPixels to WritableBitmap?

I want to display 100 remote pc monitor in my pc. and remote pc can be set pixel quality dynamic. data can be receive by rpc in my pc, just byte[] image.

2

There are 2 best solutions below

0
TsunamiCoder On

i had same writing byte by byte using following code

bitmap.Lock();
IntPtr buffer = bitmap.BackBuffer;
//Logic to write to buffer by pixel by pixel using Parallel.Foreach()
bitmap.AddDirtyRect
bitmap.UnLock()

Here the problem is sometimes before rendering actual image there is black background then actual image appears , anyone have idea regarding this

0
TsunamiCoder On
Wpf Xaml code used
 <Grid x:Name="ImageGrid" Grid.Row="1" >
                <AdornerDecorator ClipToBounds="True">
                    <Canvas x:Name="ImageCanvas" Focusable="True" 
                             Background="{StaticResource Moonstone}" 
                             Width="{Binding WindowWidth}" 
                             Height="{Binding WindowHeight}"                
                             >
                        <Image  Canvas.Left="{Binding Path=ImagePositionLeft}" 
                                Canvas.Top="{Binding Path=ImagePositionTop}"
                              
                               RenderTransformOrigin="0.1,0.1"
                               Source="{Binding ImSource}" 
                               Width="{Binding Path=ImageActualWidth}" 
                               Height="{Binding Path=ImageActualHeight}"
                               Stretch="None"
                               RenderOptions.BitmapScalingMode="{Binding  ScalingMode}">
                            <Image.RenderTransform>
                                <MatrixTransform Matrix="{Binding CurrTransform.Matrix}"></MatrixTransform>
                            </Image.RenderTransform>
                        </Image>
                    </Canvas>
                </AdornerDecorator>
            </Grid>