2 // Image class testing unit
5 // Jordi Mas i Hernàndez (jmas@softcatala.org>
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // (C) 2005 Ximian, Inc. http://www.ximian.com
9 // Copyright (C) 2005-2007 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Drawing.Imaging;
35 using System.Reflection;
36 using System.Security.Permissions;
37 using System.Xml.Serialization;
38 using NUnit.Framework;
40 namespace MonoTests.System.Drawing{
43 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
44 public class ImageTest {
47 private bool callback;
50 public void FixtureSetup ()
52 fname = Path.GetTempFileName ();
56 public void FixtureTearDown ()
72 [ExpectedException (typeof (FileNotFoundException))]
73 public void FileDoesNotExists ()
75 Image.FromFile ("FileDoesNotExists.jpg");
78 private bool CallbackTrue ()
84 private bool CallbackFalse ()
91 public void GetThumbnailImage_NullCallback_Tiff ()
93 using (Bitmap bmp = new Bitmap (10, 10)) {
94 // according to documentation a callback is mandatory
95 Image tn = bmp.GetThumbnailImage (10, 5, null, IntPtr.Zero);
96 Assert.AreEqual (5, tn.Height, "Height");
97 Assert.AreEqual (10, tn.Width, "Width");
98 Assert.IsFalse (callback, "Callback called");
99 tn.Save (fname, ImageFormat.Tiff);
104 [ExpectedException (typeof (OutOfMemoryException))]
105 public void GetThumbnailImage_Height_Zero ()
107 using (Bitmap bmp = new Bitmap (10, 10)) {
108 Image tn = bmp.GetThumbnailImage (5, 0, new Image.GetThumbnailImageAbort (CallbackFalse), IntPtr.Zero);
113 [ExpectedException (typeof (OutOfMemoryException))]
114 public void GetThumbnailImage_Width_Negative ()
116 using (Bitmap bmp = new Bitmap (10, 10)) {
117 Image tn = bmp.GetThumbnailImage (-5, 5, new Image.GetThumbnailImageAbort (CallbackFalse), IntPtr.Zero);
122 public void GetThumbnailImage_CallbackData_Invalid ()
124 using (Bitmap bmp = new Bitmap (10, 10)) {
125 // according to documentation IntPtr.Zero must be supplied as data
126 Image tn = bmp.GetThumbnailImage (5, 5, new Image.GetThumbnailImageAbort (CallbackFalse), (IntPtr)Int32.MaxValue);
127 Assert.AreEqual (5, tn.Height, "Height");
128 Assert.AreEqual (5, tn.Width, "Width");
129 Assert.IsFalse (callback, "Callback called");
130 tn.Save (fname, ImageFormat.Tiff);
135 public void GetThumbnailImage_SameSize_Bmp ()
137 using (Bitmap bmp = new Bitmap (10, 10)) {
138 Image tn = bmp.GetThumbnailImage (10, 10, new Image.GetThumbnailImageAbort (CallbackFalse), IntPtr.Zero);
139 Assert.AreEqual (10, tn.Height, "Height");
140 Assert.AreEqual (10, tn.Width, "Width");
141 Assert.IsFalse (callback, "Callback called");
142 tn.Save (fname, ImageFormat.Bmp);
147 public void GetThumbnailImage_Smaller_Gif ()
149 using (Bitmap bmp = new Bitmap (10, 10)) {
150 Image tn = bmp.GetThumbnailImage (4, 4, new Image.GetThumbnailImageAbort (CallbackTrue), IntPtr.Zero);
151 Assert.AreEqual (4, tn.Height, "Height");
152 Assert.AreEqual (4, tn.Width, "Width");
153 Assert.IsFalse (callback, "Callback called");
154 tn.Save (fname, ImageFormat.Gif);
159 public void GetThumbnailImage_Bigger_Png ()
161 using (Bitmap bmp = new Bitmap (10, 10)) {
162 Image tn = bmp.GetThumbnailImage (40, 40, new Image.GetThumbnailImageAbort (CallbackTrue), IntPtr.Zero);
163 Assert.AreEqual (40, tn.Height, "Height");
164 Assert.AreEqual (40, tn.Width, "Width");
165 Assert.IsFalse (callback, "Callback called");
166 tn.Save (fname, ImageFormat.Png);
172 [Category ("NotDotNet")] // MS 1.x throws an ArgumentNullException in this case
174 public void Stream_Unlocked ()
178 using (MemoryStream ms = new MemoryStream ()) {
179 using (Bitmap bmp = new Bitmap (10, 10)) {
180 bmp.Save (ms, ImageFormat.Png);
183 img = Image.FromStream (ms);
185 // stream isn't available anymore
186 ((Bitmap) img).MakeTransparent (Color.Transparent);
188 catch (OutOfMemoryException) {
189 int p = (int) Environment.OSVersion.Platform;
190 // libgdiplus (UNIX) doesn't lazy load the image so the
191 // stream may be freed (and this exception will never occur)
192 if ((p == 4) || (p == 128))
199 [Category ("NotDotNet")] // MS 1.x throws an ArgumentNullException in this case
201 public void Stream_Locked ()
204 using (MemoryStream ms = new MemoryStream ()) {
205 using (Bitmap bmp = new Bitmap (10, 10)) {
206 bmp.Save (ms, ImageFormat.Png);
209 img = Image.FromStream (ms);
210 // stream is available
211 ((Bitmap) img).MakeTransparent (Color.Transparent);
217 [Category ("NotWorking")] // http://bugzilla.ximian.com/show_bug.cgi?id=80558
219 [ExpectedException (typeof (InvalidOperationException))]
221 public void XmlSerialize ()
223 new XmlSerializer (typeof (Image));
226 private void Wmf (Image img)
228 Assert.IsFalse (img is Bitmap, "Bitmap");
229 Assert.IsTrue (img is Metafile, "Metafile");
231 Assert.AreEqual (327683, img.Flags, "Flags");
232 Assert.IsTrue (img.RawFormat.Equals (ImageFormat.Wmf), "Wmf");
234 Assert.IsNull (img.Tag, "Tag");
239 public void FromFile_Metafile_Wmf ()
241 string filename = TestBitmap.getInFile ("bitmaps/telescope_01.wmf");
242 using (Image img = Image.FromFile (filename)) {
248 public void FromStream_Metafile_Wmf ()
250 string filename = TestBitmap.getInFile ("bitmaps/telescope_01.wmf");
251 using (FileStream fs = File.OpenRead (filename)) {
252 using (Image img = Image.FromStream (fs)) {
258 private void Emf (Image img)
260 Assert.IsFalse (img is Bitmap, "Bitmap");
261 Assert.IsTrue (img is Metafile, "Metafile");
263 Assert.AreEqual (327683, img.Flags, "Flags");
264 Assert.IsTrue (img.RawFormat.Equals (ImageFormat.Emf), "Emf");
266 Assert.IsNull (img.Tag, "Tag");
271 public void FromFile_Metafile_Emf ()
273 string filename = TestBitmap.getInFile ("bitmaps/milkmateya01.emf");
274 using (Image img = Image.FromFile (filename)) {
280 public void FromStream_Metafile_Emf ()
282 string filename = TestBitmap.getInFile ("bitmaps/milkmateya01.emf");
283 using (FileStream fs = File.OpenRead (filename)) {
284 using (Image img = Image.FromStream (fs)) {
291 [ExpectedException (typeof (OutOfMemoryException))]
292 public void FromFile_Invalid ()
294 string filename = Assembly.GetExecutingAssembly ().Location;
295 Image.FromFile (filename);
299 [ExpectedException (typeof (ArgumentException))]
300 public void FromStream_Invalid ()
302 string filename = Assembly.GetExecutingAssembly ().Location;
303 using (FileStream fs = File.OpenRead (filename)) {
304 Image.FromStream (fs);