2009-09-29 Jeffrey Stedfast <fejj@novell.com>
[moon.git] / class / System.Windows / System.Windows.Media.Imaging / WriteableBitmap.cs
blobfe46efa4315ffd0ed26f3c04dbfdf5ee1f8d98d2
1 //
2 // BitmapImage.cs
3 //
4 // Copyright 2008 Novell, Inc.
5 //
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:
13 //
14 // The above copyright notice and this permission notice shall be
15 // included in all copies or substantial portions of the Software.
16 //
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.
26 using System.Windows;
27 using System.Windows.Controls;
28 using System.Windows.Media;
29 using System.Windows.Resources;
30 using System.IO;
31 using System.Threading;
32 using System.Net;
33 using System.Runtime.InteropServices;
34 using Mono;
36 namespace System.Windows.Media.Imaging
38 public partial class WriteableBitmap : BitmapSource
40 int[] pixels;
41 GCHandle pixels_handle;
43 public WriteableBitmap (BitmapSource source) : base (NativeMethods.writeable_bitmap_new (), true)
45 if (source != null)
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
54 // bitmapsource.
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)
69 PixelWidth = width;
70 PixelHeight = height;
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);
84 ~WriteableBitmap ()
86 if (pixels_handle.IsAllocated) {
87 pixels_handle.Free ();
91 public int[] Pixels {
92 get { return pixels; }
95 public void Render (UIElement element, Transform transform)
97 if (element == null)
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);