4 // Copyright 2008 Novell, Inc.
6 // Permission is hereby granted, free of charge, to any person obtaining
7 // a copy of this software and associated documentation files (the
8 // "Software"), to deal in the Software without restriction, including
9 // without limitation the rights to use, copy, modify, merge, publish,
10 // distribute, sublicense, and/or sell copies of the Software, and to
11 // permit persons to whom the Software is furnished to do so, subject to
12 // the following conditions:
14 // The above copyright notice and this permission notice shall be
15 // included in all copies or substantial portions of the Software.
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 using System
.Windows
.Controls
;
28 using System
.Windows
.Media
;
29 using System
.Windows
.Resources
;
31 using System
.Threading
;
33 using System
.Runtime
.InteropServices
;
36 namespace System
.Windows
.Media
.Imaging
38 public partial class WriteableBitmap
: BitmapSource
41 GCHandle pixels_handle
;
43 public WriteableBitmap (BitmapSource source
) : base (NativeMethods
.writeable_bitmap_new (), true)
46 NativeMethods
.writeable_bitmap_initialize_from_bitmap_source (native
, source
.native
);
48 // FIXME: we need to test if modifications
49 // done to the WB update the BitmapSource's
50 // pixel data. If they do, we can't use this
51 // copying code and need to do more
52 // complicated tricks to get the pixels array
53 // to reflect the contents of the
56 // FIXME: we aren't taking row stride into
57 // account here. we're assuming the pixel
58 // data of the source is 32 bits.
59 pixels
= new int[source
.PixelWidth
* source
.PixelHeight
];
60 IntPtr bitmap_data
= NativeMethods
.bitmap_source_get_bitmap_data (source
.native
);
61 if (bitmap_data
!= IntPtr
.Zero
)
62 Marshal
.Copy (bitmap_data
, pixels
, 0, pixels
.Length
);
64 PinAndSetBitmapData ();
67 public WriteableBitmap (int width
, int height
) : base (NativeMethods
.writeable_bitmap_new (), true)
72 pixels
= new int[PixelWidth
* PixelHeight
];
74 PinAndSetBitmapData ();
77 void PinAndSetBitmapData ()
79 pixels_handle
= GCHandle
.Alloc(pixels
, GCHandleType
.Pinned
); // pin it so it doesn't move
81 NativeMethods
.bitmap_source_set_bitmap_data (native
, pixels_handle
.AddrOfPinnedObject(), false);
86 if (pixels_handle
.IsAllocated
) {
87 pixels_handle
.Free ();
92 get { return pixels; }
95 public void Render (UIElement element
, Transform transform
)
98 throw new NullReferenceException ("element cannot be null");
99 if (transform
== null)
100 throw new NullReferenceException ("transform cannot be null");
102 NativeMethods
.writeable_bitmap_render (native
, element
.native
, transform
.native
);
105 public void Invalidate ()
107 NativeMethods
.bitmap_source_invalidate (native
);