2 // System.Drawing.Imaging.ImageAttributes.cs
5 // Dennis Hayes (dennish@raytek.com) (stubbed out)
6 // Jordi Mas i Hernàndez (jmas@softcatala.org)
7 // Sanjay Gupta (gsanjay@novell.com)
9 // (C) 2002-4 Ximian, Inc. http://www.ximian.com
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 using System
.Drawing
.Drawing2D
;
38 using System
.Runtime
.InteropServices
;
40 namespace System
.Drawing
.Imaging
43 /// Summary description for ImageAttributes.
45 public sealed class ImageAttributes
: ICloneable
, IDisposable
{
47 private IntPtr nativeImageAttr
= IntPtr
.Zero
;
49 internal IntPtr NativeObject
{
51 return nativeImageAttr
;
55 internal ImageAttributes(IntPtr native
)
57 nativeImageAttr
= native
;
60 public ImageAttributes() {
62 Status status
= GDIPlus
.GdipCreateImageAttributes(out nativeImageAttr
);
64 if (status
!= Status
.Ok
)
65 throw new Exception ("Error calling GDIPlus.GdipCreateImageAttributes:" +status
);
69 public void ClearBrushRemapTable()
71 ClearRemapTable (ColorAdjustType
.Brush
);
74 //Clears the color keys for all GDI+ objects
75 public void ClearColorKey()
77 ClearColorKey (ColorAdjustType
.Default
);
80 public void ClearColorKey(ColorAdjustType type
)
82 Status status
= GDIPlus
.GdipSetImageAttributesColorKeys (nativeImageAttr
,
85 GDIPlus
.CheckStatus (status
);
88 public void ClearColorMatrix()
90 ClearColorMatrix (ColorAdjustType
.Default
);
93 public void ClearColorMatrix(ColorAdjustType type
)
95 Status status
= GDIPlus
.GdipSetImageAttributesColorMatrix (nativeImageAttr
,
96 type
, false, null, null, ColorMatrixFlag
.Default
);
98 GDIPlus
.CheckStatus (status
);
101 public void ClearGamma()
103 ClearGamma (ColorAdjustType
.Default
);
106 public void ClearGamma(ColorAdjustType type
)
108 Status status
= GDIPlus
.GdipSetImageAttributesGamma (nativeImageAttr
, type
, false, 0);
110 GDIPlus
.CheckStatus (status
);
113 public void ClearNoOp()
115 ClearNoOp (ColorAdjustType
.Default
);
118 public void ClearNoOp(ColorAdjustType type
)
120 Status status
= GDIPlus
.GdipSetImageAttributesNoOp (nativeImageAttr
, type
, false);
122 GDIPlus
.CheckStatus (status
);
125 public void ClearOutputChannel()
127 ClearOutputChannel (ColorAdjustType
.Default
);
130 public void ClearOutputChannel(ColorAdjustType type
)
132 Status status
= GDIPlus
.GdipSetImageAttributesOutputChannel (nativeImageAttr
,
133 type
, false, ColorChannelFlag
.ColorChannelLast
);
135 GDIPlus
.CheckStatus (status
);
138 public void ClearOutputChannelColorProfile()
140 ClearOutputChannelColorProfile (ColorAdjustType
.Default
);
143 public void ClearOutputChannelColorProfile(ColorAdjustType type
)
145 Status status
= GDIPlus
.GdipSetImageAttributesOutputChannelColorProfile (nativeImageAttr
,
148 GDIPlus
.CheckStatus (status
);
151 public void ClearRemapTable()
153 ClearRemapTable (ColorAdjustType
.Default
);
156 public void ClearRemapTable(ColorAdjustType type
)
158 Status status
= GDIPlus
.GdipSetImageAttributesRemapTable (nativeImageAttr
,
159 type
, false, 0, IntPtr
.Zero
);
161 GDIPlus
.CheckStatus (status
);
164 public void ClearThreshold()
166 ClearThreshold (ColorAdjustType
.Default
);
169 public void ClearThreshold(ColorAdjustType type
)
171 Status status
= GDIPlus
.GdipSetImageAttributesThreshold (nativeImageAttr
,
174 GDIPlus
.CheckStatus (status
);
177 //Sets the color keys for all GDI+ objects
178 public void SetColorKey(Color colorLow
, Color colorHigh
)
181 Status status
= GDIPlus
.GdipSetImageAttributesColorKeys(nativeImageAttr
,
182 ColorAdjustType
.Default
, true, colorLow
.ToArgb(), colorHigh
.ToArgb());
184 if (status
!= Status
.Ok
)
185 throw new Exception ("Error calling GDIPlus.GdipSetImageAttributesColorKeys:" +status
);
188 public void SetColorMatrix(ColorMatrix colorMatrix
)
191 Status status
= GDIPlus
.GdipSetImageAttributesColorMatrix(nativeImageAttr
, ColorAdjustType
.Default
,
192 true, colorMatrix
, (ColorMatrix
)null, ColorMatrixFlag
.Default
);
194 if (status
!= Status
.Ok
)
195 throw new Exception ("Error calling GDIPlus.SetColorMatrix:" +status
);
198 public void SetColorMatrix(ColorMatrix colorMatrix
, ColorMatrixFlag colorMatrixFlag
)
201 Status status
= GDIPlus
.GdipSetImageAttributesColorMatrix(nativeImageAttr
, ColorAdjustType
.Default
,
202 true, colorMatrix
, (ColorMatrix
)null, colorMatrixFlag
);
203 if (status
!= Status
.Ok
)
204 throw new Exception ("Error calling GDIPlus.SetColorMatrix:" +status
);
208 public void SetColorMatrix(ColorMatrix colorMatrix
, ColorMatrixFlag colorMatrixFlag
, ColorAdjustType colorAdjustType
) {
210 Status status
= GDIPlus
.GdipSetImageAttributesColorMatrix(nativeImageAttr
,colorAdjustType
,
211 true, colorMatrix
, (ColorMatrix
)null, colorMatrixFlag
);
213 if (status
!= Status
.Ok
)
214 throw new Exception ("Error calling GDIPlus.SetColorMatrix:" +status
);
217 void Dispose (bool disposing
)
219 if (!disposing
) return;
221 Status status
= GDIPlus
.GdipDisposeImageAttributes(nativeImageAttr
);
223 if (status
!= Status
.Ok
)
224 throw new Exception ("Error calling GDIPlus.GdipDisposeImageAttributes:" +status
);
226 nativeImageAttr
= IntPtr
.Zero
;
230 public void Dispose()
234 System
.GC
.SuppressFinalize (this);
245 public object Clone()
249 Status status
= GDIPlus
.GdipCloneImageAttributes (nativeImageAttr
, out imgclone
);
250 GDIPlus
.CheckStatus (status
);
252 return new ImageAttributes (imgclone
);
255 public void GetAdjustedPalette(ColorPalette palette
, ColorAdjustType type
)
259 Status status
= GDIPlus
.GdipGetImageAttributesAdjustedPalette (nativeImageAttr
,
260 out colorPalette
, type
);
262 palette
.setFromGDIPalette (colorPalette
);
266 public void SetBrushRemapTable(ColorMap
[] map
)
268 GdiColorMap gdiclr
= new GdiColorMap ();
269 IntPtr clrmap
, lpPointer
;
270 int mapsize
= Marshal
.SizeOf (gdiclr
);
271 int size
= mapsize
* map
.Length
;
272 clrmap
= lpPointer
= Marshal
.AllocHGlobal (size
);
274 for (int i
=0; i
< map
.Length
; i
++)
276 gdiclr
.from = map
[i
].OldColor
.ToArgb();
277 gdiclr
.to
= map
[i
].NewColor
.ToArgb();
279 Marshal
.StructureToPtr (gdiclr
, lpPointer
, false);
280 lpPointer
= (IntPtr
) (lpPointer
.ToInt32() + mapsize
);
283 Status status
= GDIPlus
.GdipSetImageAttributesRemapTable (nativeImageAttr
,
284 ColorAdjustType
.Brush
, true, (uint) map
.Length
, clrmap
);
286 Marshal
.FreeHGlobal (clrmap
);
288 GDIPlus
.CheckStatus (status
);
292 public void SetColorKey(Color colorLow
, Color colorHigh
, ColorAdjustType type
)
294 Status status
= GDIPlus
.GdipSetImageAttributesColorKeys (nativeImageAttr
,
295 type
, true, colorLow
.ToArgb (), colorHigh
.ToArgb ());
297 GDIPlus
.CheckStatus (status
);
301 public void SetColorMatrices(ColorMatrix newColorMatrix
, ColorMatrix grayMatrix
)
303 Status status
= GDIPlus
.GdipSetImageAttributesColorMatrix (nativeImageAttr
,
304 ColorAdjustType
.Default
, true, newColorMatrix
, grayMatrix
, ColorMatrixFlag
.Default
);
306 GDIPlus
.CheckStatus (status
);
309 public void SetColorMatrices(ColorMatrix newColorMatrix
, ColorMatrix grayMatrix
, ColorMatrixFlag flags
)
311 Status status
= GDIPlus
.GdipSetImageAttributesColorMatrix (nativeImageAttr
,
312 ColorAdjustType
.Default
, true, newColorMatrix
, grayMatrix
, flags
);
314 GDIPlus
.CheckStatus (status
);
317 public void SetColorMatrices(ColorMatrix newColorMatrix
, ColorMatrix grayMatrix
, ColorMatrixFlag mode
, ColorAdjustType type
)
319 Status status
= GDIPlus
.GdipSetImageAttributesColorMatrix (nativeImageAttr
,
320 ColorAdjustType
.Default
, true, newColorMatrix
, grayMatrix
, mode
);
322 GDIPlus
.CheckStatus (status
);
325 public void SetGamma(float gamma
)
327 SetGamma (gamma
, ColorAdjustType
.Default
);
330 public void SetGamma(float gamma
, ColorAdjustType coloradjust
)
332 Status status
= GDIPlus
.GdipSetImageAttributesGamma (nativeImageAttr
, coloradjust
, true,
335 GDIPlus
.CheckStatus (status
);
338 public void SetNoOp()
340 SetNoOp (ColorAdjustType
.Default
);
343 public void SetNoOp(ColorAdjustType type
)
345 Status status
= GDIPlus
.GdipSetImageAttributesNoOp (nativeImageAttr
,
348 GDIPlus
.CheckStatus (status
);
351 public void SetOutputChannel(ColorChannelFlag flags
)
353 SetOutputChannel (flags
, ColorAdjustType
.Default
);
356 public void SetOutputChannel(ColorChannelFlag flags
, ColorAdjustType type
)
358 Status status
= GDIPlus
.GdipSetImageAttributesOutputChannel (nativeImageAttr
,
361 GDIPlus
.CheckStatus (status
);
364 public void SetOutputChannelColorProfile(string colorProfileFilename
)
366 SetOutputChannelColorProfile (colorProfileFilename
, ColorAdjustType
.Default
);
369 public void SetOutputChannelColorProfile(string colorProfileFilename
, ColorAdjustType type
)
371 Status status
= GDIPlus
.GdipSetImageAttributesOutputChannelColorProfile (nativeImageAttr
,
372 type
, true, colorProfileFilename
);
374 GDIPlus
.CheckStatus (status
);
377 public void SetRemapTable(ColorMap
[] map
)
379 SetRemapTable (map
, ColorAdjustType
.Default
);
383 public void SetRemapTable(ColorMap
[] map
, ColorAdjustType type
)
385 GdiColorMap gdiclr
= new GdiColorMap ();
386 IntPtr clrmap
, lpPointer
;
387 int mapsize
= Marshal
.SizeOf (gdiclr
);
388 int size
= mapsize
* map
.Length
;
389 clrmap
= lpPointer
= Marshal
.AllocHGlobal (size
);
391 for (int i
=0; i
< map
.Length
; i
++)
393 gdiclr
.from = map
[i
].OldColor
.ToArgb();
394 gdiclr
.to
= map
[i
].NewColor
.ToArgb();
396 Marshal
.StructureToPtr (gdiclr
, lpPointer
, false);
397 lpPointer
= (IntPtr
) (lpPointer
.ToInt32() + mapsize
);
400 Status status
= GDIPlus
.GdipSetImageAttributesRemapTable (nativeImageAttr
,
401 type
, true, (uint) map
.Length
, clrmap
);
403 Marshal
.FreeHGlobal (clrmap
);
405 GDIPlus
.CheckStatus (status
);
409 public void SetThreshold(float threshold
)
411 SetThreshold (threshold
, ColorAdjustType
.Default
);
414 public void SetThreshold(float threshold
, ColorAdjustType type
)
416 Status status
= GDIPlus
.GdipSetImageAttributesThreshold (nativeImageAttr
,
419 GDIPlus
.CheckStatus (status
);
422 public void SetWrapMode(WrapMode mode
)
424 SetWrapMode (mode
, Color
.Black
);
428 public void SetWrapMode(WrapMode mode
, Color color
)
430 SetWrapMode (mode
, color
, false);
434 public void SetWrapMode(WrapMode mode
, Color color
, bool clamp
)
436 Status status
= GDIPlus
.GdipSetImageAttributesWrapMode (nativeImageAttr
, mode
,
437 color
.ToArgb(), clamp
);
439 GDIPlus
.CheckStatus (status
);