5
// Lluis Sanchez <lluis@xamarin.com>
7
// Copyright (c) 2011 Xamarin Inc
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30
using System.Reflection;
35
public sealed class Image: XwtObject, IDisposable
37
static ImageBackendHandler handler;
41
handler = WidgetRegistry.CreateSharedBackend<ImageBackendHandler> (typeof(Image));
44
protected override IBackendHandler BackendHandler {
50
internal Image (object backend): base (backend)
54
public Image (Image image): base (handler.Copy (image.Backend))
58
public static Image FromResource (Type type, string resource)
60
var img = handler.LoadFromResource (type.Assembly, resource);
62
throw new InvalidOperationException ("Resource not found: " + resource);
63
return new Image (img);
66
public static Image FromResource (Assembly asm, string resource)
68
var img = handler.LoadFromResource (asm, resource);
70
throw new InvalidOperationException ("Resource not found: " + resource);
71
return new Image (img);
74
public static Image FromFile (string file)
76
return new Image (handler.LoadFromFile (file));
79
public static Image FromStream (Stream stream)
81
return new Image (handler.LoadFromStream (stream));
84
public static Image FromIcon (string id, IconSize size)
86
return new Image (handler.LoadFromIcon (id, size));
90
get { return handler.GetSize (Backend); }
93
public void SetPixel (int x, int y, Color color)
95
handler.SetPixel (Backend, x, y, color);
98
public Color GetPixel (int x, int y)
100
return handler.GetPixel (Backend, x, y);
103
public Image Scale (double scale)
105
double w = Size.Width * scale;
106
double h = Size.Height * scale;
107
return new Image (handler.Resize (Backend, w, h));
110
public Image Scale (double scaleX, double scaleY)
112
double w = Size.Width * scaleX;
113
double h = Size.Height * scaleY;
114
return new Image (handler.Resize (Backend, w, h));
117
public Image Resize (double width, double height)
119
return new Image (handler.Resize (Backend, width, height));
122
public Image ResizeToFitBox (double width, double height)
124
double r = Math.Min (width / Size.Width, height / Size.Height);
125
return new Image (handler.Resize (Backend, Size.Width * r, Size.Height * r));
128
public Image ToGrayscale ()
130
throw new NotImplementedException ();
133
public Image ChangeOpacity (double opacity)
135
return new Image (handler.ChangeOpacity (Backend, opacity));
138
public void CopyArea (int srcX, int srcY, int width, int height, Image dest, int destX, int destY)
140
handler.CopyArea (Backend, srcX, srcY, width, height, dest.Backend, destX, destY);
143
public Image Crop (int srcX, int srcY, int width, int height)
145
return new Image (handler.Crop (Backend, srcX, srcY, width, height));
148
public void Dispose ()
150
handler.Dispose (Backend);