[Moonlight-list] Res: Help with WriteableBitmap
Geoff Norton
gnorton at novell.com
Wed Jul 22 14:12:53 EDT 2009
Tony,
There is likely a bug in my code setting PixelWidth and PixelHeight
from existing images since I havn't udpated to the RTM code yet. Can
you file a bug with a testcase showing the problems you're having?
Thanks
-g
On 21-Jul-09, at 5:44 PM, Tony Alexander Hild wrote:
> Hi,
>
> The path is right. The image is rendering because is marked as
> resource.
>
> What I'm trying to do is change this image with WriteableBitmap. The
> SL 3 RTM have breaking changes, and Moonligth WriteableBitmap is
> compatible with SL Beta. In SL Beta version was not allowed to write
> in an image that was already rendered, but now is possible. The
> acess to pixels was changed from an indexer to a property that
> returns an array int[], and now it's not possible to acess the pixel
> through Marshal.ReadInt32 since we don't know the offset. I tried
> to using the Marshal.Copy but I get an Security Access error.
>
> I don't know how cast from IntPtr (buffer) directly to an int[].
>
> Another issue is that the BitmapSource return 0 from the PixelWidth
> and PixelHeight properties. I started digging the C++ code to figure
> out how the things works.
>
> Tony
>
> De: Diego Frata <diego.frata at gmail.com>
> Para: Tony Alexander Hild <tony_hild at yahoo.com>
> Enviadas: Terça-feira, 21 de Julho de 2009 18:07:18
> Assunto: Re: [Moonlight-list] Help with WriteableBitmap
>
> Hello,
>
> I have some experience with WriteableBitmap and BitmapSource on WPF
> (not on Mono). Maybe I could help you with this implementation.
>
> I was looking at your code and it raised me a question: Can you
> confirm your instance of BitmapSource points to a valid file? The
> path you entered doesn't sound right to me Source="/test;component/
> 2.png".
>
> The image control is rendering the image correctly?
>
> Sorry if it's a dumb question, but I'll try to look at the source
> code later (i'm at work right now).
>
> Diego Frata
> diego.frata at gmail.com
>
>
> On Tue, Jul 21, 2009 at 3:33 PM, Tony Alexander Hild <tony_hild at yahoo.com
> > wrote:
> 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 zero
>
> Exception 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: Top 10 -
> Celebridades - Música - Esportes
>
> _______________________________________________
> Moonlight-list mailing list
> Moonlight-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/moonlight-list
>
>
>
> Veja quais são os assuntos do momento no Yahoo! + Buscados: Top 10 -
> Celebridades - Música -
> Esportes_______________________________________________
> Moonlight-list mailing list
> Moonlight-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/moonlight-list
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/moonlight-list/attachments/20090722/1a4e7d55/attachment-0001.html
More information about the Moonlight-list
mailing list