download Bitmap.cs
Language: C#
License: GPL
Copyright: (C) 2003 Southern Storm Software, Pty Ltd.
LOC: 252
Project Info
DotGNU Portable.NET(dotgnu-pnet)
Server: Savannah GNU
Type: cvs
...net\pnetlib\System.Drawing\
   .cvsignore
   Bitmap.cs
   Brush.cs
   Brushes.cs
   CharacterRange.cs
   Color.cs
   ColorConverter.cs
   ColorTranslator.cs
   ContentAlignment.cs
   Font.cs
   FontConverter.cs
   FontFamily.cs
   FontStyle.cs
   Graphics.cs
   GraphicsUnit.cs
   Icon.cs
   IconConverter.cs
   Image.cs
   ImageAnimator.cs
   ImageConverter.cs
   ImageFormatConverter.cs
   KnownColor.cs
   Makefile.am
   Pen.cs
   Pens.cs
   Point.cs
   PointConverter.cs
   PointF.cs
   Rectangle.cs
   RectangleConverter.cs
   RectangleF.cs
   Region.cs
   RotateFlipType.cs
   S.cs
   Size.cs
   SizeConverter.cs
   SizeF.cs
   SolidBrush.cs
   StringAlignment.cs
   StringDigitSubstitute.cs
   StringFormat.cs
   StringFormatFlags.cs
   StringTrimming.cs
   StringUnit.cs
   System.Drawing.build
   SystemBrushes.cs
   SystemColors.cs
   SystemIcons.cs
   SystemPens.cs
   TextureBrush.cs
   TODOAttribute.cs
   ToolboxBitmapAttribute.cs
   XorBrush.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
 * Bitmap.cs - Implementation of the "System.Drawing.Bitmap" class.
 *
 * Copyright (C) 2003  Southern Storm Software, Pty Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

namespace System.Drawing
{

using System.IO;
using System.Security;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Drawing.Imaging;
using System.Drawing.Design;
using System.ComponentModel;
using DotGNU.Images;
using System.Drawing.Toolkit;

#if !ECMA_COMPAT
[Serializable]
[ComVisible(true)]
#endif
#if CONFIG_COMPONENT_MODEL_DESIGN
[Editor("System.Drawing.Design.BitmapEditor, System.Drawing.Design",
		typeof(UITypeEditor))]
#endif
public sealed class Bitmap : System.Drawing.Image
{
	// Constructors.
	public Bitmap(Image original)
			: this(original, original.Width, original.Height) {}
	public Bitmap(Image original, Size newSize)
			: this (original, newSize.Width, newSize.Height) {}
	public Bitmap(Stream stream) : this(stream, false) {}
	public Bitmap(Stream stream, bool useIcm)
			{
				DotGNU.Images.Image dgImage = new DotGNU.Images.Image();
				dgImage.Load(stream);
				SetDGImage(dgImage);
			}
	public Bitmap(String filename) : this(filename, false) {}
	public Bitmap(String filename, bool useIcm)
			{
				DotGNU.Images.Image dgImage = new DotGNU.Images.Image();
				dgImage.Load(filename);
				SetDGImage(dgImage);
			}
	public Bitmap(int width, int height)
			: this(width, height, Imaging.PixelFormat.Format24bppRgb) {}
	public Bitmap(int width, int height,
				  System.Drawing.Imaging.PixelFormat format)
			{
				SetDGImage(new DotGNU.Images.Image
					(width, height, (DotGNU.Images.PixelFormat)format));
				dgImage.AddFrame();
			}
	public Bitmap(int width, int height, Graphics g)
			{
				if(g == null)
				{
					throw new ArgumentNullException("g");
				}
				SetDGImage(new DotGNU.Images.Image
					(width, height, DotGNU.Images.PixelFormat.Format24bppRgb));
				dgImage.AddFrame();
			}
	public Bitmap(Type type, String resource)
			{
				Stream stream = GetManifestResourceStream(type, resource);
				if(stream == null)
				{
					throw new ArgumentException(S._("Arg_UnknownResource"));
				}
				try
				{
					DotGNU.Images.Image dgImage = new DotGNU.Images.Image();
					dgImage.Load(stream);
					SetDGImage(dgImage);
				}
				finally
				{
					stream.Close();
				}
			}
	public Bitmap(Image original, int width, int height)
			{
				if(original.dgImage != null)
				{
					SetDGImage(original.dgImage.Stretch(width, height));
				}
			}
	public Bitmap(int width, int height, int stride,
				  System.Drawing.Imaging.PixelFormat format, IntPtr scan0)
			{
				// We don't support loading bitmaps from unmanaged buffers.
				throw new SecurityException();
			}
	internal Bitmap(DotGNU.Images.Image image) : base(image) {}
#if CONFIG_SERIALIZATION
	internal Bitmap(SerializationInfo info, StreamingContext context)
			: base(info, context) {}
#endif

	// Get a manifest resource stream.  Profile-safe version.
	internal static Stream GetManifestResourceStream(Type type, String name)
			{
			#if !ECMA_COMPAT
				return type.Module.Assembly.GetManifestResourceStream
						(type, name);
			#else
				if(type.Namespace != null && type.Namespace != String.Empty)
				{
					return type.Module.Assembly.GetManifestResourceStream
						(type.Namespace + "." + name);
				}
				return type.Module.Assembly.GetManifestResourceStream(name);
			#endif
			}

	// Clone this bitmap and transform it into a new pixel format
	[TODO]
	public Bitmap Clone
				(Rectangle rect, System.Drawing.Imaging.PixelFormat format)
			{
				// TODO : There has to be a better way !!
				Bitmap b = new Bitmap(rect.Width, rect.Height, format);
				for(int x = 0 ; x < rect.Width ; x++)
				{
					for(int y = 0 ; y < rect.Height ; y++)
					{
						b.SetPixel(x,y, GetPixel(rect.Left+x,rect.Top+y));
					}
				}
				return b;
			}
	[TODO]
	public Bitmap Clone
				(RectangleF rect, System.Drawing.Imaging.PixelFormat format)
			{
				// TODO : There has to be a better way !!
				Bitmap b = new Bitmap((int)rect.Width, (int)rect.Height, format);
				for(int x = 0 ; x < rect.Width ; x++)
				{
					for(int y = 0 ; y < rect.Height ; y++)
					{
						b.SetPixel(x,y,
								GetPixel((int)rect.Left+x, (int)rect.Top+y));
					}
				}
				return b;
			}

	// Create a bitmap from a native icon handle.
	public static Bitmap FromHicon(IntPtr hicon)
			{
				throw new SecurityException();
			}

	// Create a bitmap from a Windows resource name.
	public static Bitmap FromResource(IntPtr hinstance, String bitmapName)
			{
				throw new SecurityException();
			}

	// Convert this bitmap into a native bitmap handle.
#if CONFIG_COMPONENT_MODEL
	[EditorBrowsable(EditorBrowsableState.Advanced)]
#endif
	public IntPtr GetHbitmap()
			{
				return GetHbitmap(Color.LightGray);
			}
	[TODO]
#if CONFIG_COMPONENT_MODEL
	[EditorBrowsable(EditorBrowsableState.Advanced)]
#endif
	public IntPtr GetHbitmap(Color background)
			{
				throw new SecurityException();
			}

	// Convert this bitmap into a native icon handle.
	[TODO]
#if CONFIG_COMPONENT_MODEL
	[EditorBrowsable(EditorBrowsableState.Advanced)]
#endif
	public IntPtr GetHicon()
			{
				throw new SecurityException();
			}

	// Get the color of a specific pixel.
	public Color GetPixel(int x, int y)
			{
				if(dgImage != null)
				{
					int pix = dgImage.GetFrame(0).GetPixel(x, y);
					return Color.FromArgb((pix >> 16) & 0xFF,
										  (pix >> 8) & 0xFF,
										  pix & 0xFF);
				}
				return Color.Empty;
			}

	// Lock a region of this bitmap.  Use of this method is discouraged.
	// It assumes that managed arrays are fixed in place in memory,
	// which is true for ilrun, but maybe not other CLR implementations.
	// We also assume that "format" is the same as the bitmap's real format.
	public unsafe BitmapData LockBits
					(Rectangle rect, ImageLockMode flags,
					 System.Drawing.Imaging.PixelFormat format)
			{
				BitmapData bitmapData = new BitmapData();
				bitmapData.Width = rect.Width;
				bitmapData.Height = rect.Height;
				bitmapData.PixelFormat = format;
				if(dgImage != null)
				{
					Frame frame = dgImage.GetFrame(0);
					if(frame != null)
					{
						if (format != this.PixelFormat)
						{
							frame = frame.Reformat((DotGNU.Images.PixelFormat) format);
						}
						bitmapData.Stride = frame.Stride;
						byte[] data = frame.Data;
						bitmapData.dataHandle = GCHandle.Alloc(data);

						int offset = rect.X * GetPixelFormatSize(format) / 8;
						// TODO: will GCHandle.AddrOfPinnedObject work more 
						//       portably across GCs ?
						fixed (byte *pixel = &(data[rect.Y * frame.Stride]))
						{
							bitmapData.Scan0 = (IntPtr)(void *)(pixel + offset);
						}
					}
				}
				return bitmapData;
			}

	// Make a particular color transparent within this bitmap.
	public void MakeTransparent()
			{
				Color transparentColor = Color.LightGray;
				if(Width > 1 && Height > 1)
				{
					transparentColor = GetPixel(0, Height - 1);
					if(transparentColor.A == 0xFF)
					{
						// Use light grey
						transparentColor = Color.LightGray;
					}
				}
				MakeTransparent(transparentColor);
			}
	public void MakeTransparent(Color transparentColor)
			{
				// Make all the frames transparent.
				for (int f = 0; f < dgImage.NumFrames; f++)
				{
					Frame frame = dgImage.GetFrame(f);
					int color = transparentColor.ToArgb();
					if(!Image.IsAlphaPixelFormat(PixelFormat))
					{
						// Remove the alpha component.
						color = color & 0x00FFFFFF;
					}
					frame.MakeTransparent(color);
				}
			}

	// Set a pixel within this bitmap.
	public void SetPixel(int x, int y, Color color)
			{
				if(dgImage != null)
				{
					dgImage.GetFrame(0).SetPixel
						(x, y, (color.R << 16) | (color.G << 8) | color.B);
				}
			}

	// Set the resolution for this bitmap.
	public void SetResolution(float dpiX, float dpiY)
			{
				horizontalResolution = dpiX;
				verticalResolution = dpiY;
			}

	// Unlock the bits within this bitmap.
	public void UnlockBits(BitmapData bitmapData)
			{
				// Nothing to do in this implementation.
				bitmapData.dataHandle.Free();
			}

}; // class Bitmap

}; // namespace System.Drawing

About Koders | Resources | Downloads | Support | Black Duck | Submit Project | Terms of Service | DMCA | Privacy Policy | Site Map| Contact Us