[Moonlight-list] Help with WriteableBitmap

Tony Alexander Hild tony_hild at yahoo.com
Tue Jul 21 14:33:23 EDT 2009


Hi,

I have made a simple program to test the WriteableBitmap.
btnTest_Click calls the Invert function.


             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
             Width="1000" Height="650"
             x:Class="HelloMoon.Page"
>
    <Grid x:Name="LayoutRoot" Background="Black">
        <TextBlock x:Name="tblText" FontSize="30" Foreground="White">Hello Moon!</TextBlock>
    <Button x:Name="btnTest" Width="200" VerticalAlignment="Top" Height="50" Canvas.Top="0" Canvas.Left="100" Click="btnTest_Click" Content="Click me!"></Button>
    <Image x:Name="MyBitmap" Source="/test;component/2.png"
               Width="512"
               Height="512" />

    </Grid>
</UserControl>

        private void Invert ()
        {
            const int imageWidth = 512;
            const int imageHeight = 512;

            WriteableBitmap b = new WriteableBitmap ((BitmapSource)MyBitmap.Source);
            //WriteableBitmap b = new WriteableBitmap (imageWidth, imageHeight, PixelFormats.Bgr32);

            for (int x = 0; x < imageWidth; x++) {
                for (int y = 0; y < imageHeight; y++) {
                    int color = b.Pixels[y * imageWidth + x];
                    if (color == 0) {
                        // generate a color in Pbgra32 format
                        byte[] components = {
                            255,
                            255,
                            255,
                            0
                        };
                        int pixelValue = BitConverter.ToInt32 (components, 0);
                        b.Pixels[y * imageWidth + x] = pixelValue;

                    } else {
                        b.Pixels[y * imageWidth + x] = b.Pixels[y * imageWidth + x] = 0;
                    }
                }
            }

            b.Invalidate ();
            MyBitmap.Source = b;

        }


I also modified the constructor of WriteableBitmap:

        public WriteableBitmap (BitmapSource source) : base(NativeMethods.writeable_bitmap_new (), true)
        {
            rendered = true;
            PixelWidth = source.PixelWidth;
            PixelHeight = source.PixelHeight;
            PixelFormat = PixelFormats.Pbgra32;
            if (source != null)
                NativeMethods.writeable_bitmap_initialize_from_bitmap_source (native, source.native);
        }

But source.PixelWidth/Height always is 0, so i get this error:


Value must be greater than zeroException Details: System.ArgumentException: Value must be greater than zero

Stack Trace:


   at Mono.NativeMethods.CreateManagedException (MoonError err) [0x00000] 
   at Mono.NativeMethods.dependency_object_set_value (IntPtr instance, IntPtr property, Mono.Value& value) [0x00000] 
   at Mono.NativeDependencyObjectHelper.SetValue (INativeDependencyObjectWrapper wrapper, System.Windows.DependencyProperty dp, System.Object value) [0x00000] 
   at System.Windows.DependencyObject.SetValueImpl (System.Windows.DependencyProperty dp, System.Object value) [0x00000] 
   at System.Windows.DependencyObject.SetValue (System.Windows.DependencyProperty dp, System.Object value) [0x00000] 
   at System.Windows.Media.Imaging.BitmapSource.set_PixelWidth (Int32 value) [0x00000] 
   at System.Windows.Media.Imaging.WriteableBitmap..ctor (System.Windows.Media.Imaging.BitmapSource source) [0x00000] 
   at HelloMoon.Page.Invert () [0x00000] 
   at HelloMoon.Page.btnTest_Click (System.Object sender, System.Windows.RoutedEventArgs e) [0x00000] 
   at System.Windows.Controls.Primitives.ButtonBase.OnClick () [0x00000] 
   at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp (System.Windows.Input.MouseButtonEventArgs e) [0x00000] 
   at System.Windows.Controls.Control.InvokeMouseLeftButtonUp (System.Windows.Input.MouseButtonEventArgs e) [0x00000] 
   at Mono.Events.mouse_left_button_up_callback (IntPtr target, IntPtr calldata, IntPtr closure) [0x00000] 
   at Mono.Events+<CreateSafeHandler>c__AnonStorey1.<>m__2 (IntPtr a, IntPtr b, IntPtr c) [0x00000]  
But as the BitmapSource is already rendered, it should have the Height and Width properties set.

What I'm doing wrong?
I'm using trunk version, from mono, mcs, libgdi and moon.


Tony



      ____________________________________________________________________________________
Veja quais são os assuntos do momento no Yahoo! +Buscados
http://br.maisbuscados.yahoo.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/moonlight-list/attachments/20090721/e4d6fc42/attachment.html 


More information about the Moonlight-list mailing list