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 public void FileDoesNotExists ()
74 Assert.Throws<FileNotFoundException> (() => Image.FromFile ("FileDoesNotExists.jpg"));
77 private bool CallbackTrue ()
83 private bool CallbackFalse ()
90 public void GetThumbnailImage_NullCallback_Tiff ()
92 using (Bitmap bmp = new Bitmap (10, 10)) {
93 // according to documentation a callback is mandatory
94 Image tn = bmp.GetThumbnailImage (10, 5, null, IntPtr.Zero);
95 Assert.AreEqual (5, tn.Height, "Height");
96 Assert.AreEqual (10, tn.Width, "Width");
97 Assert.IsFalse (callback, "Callback called");
98 tn.Save (fname, ImageFormat.Tiff);
103 public void GetThumbnailImage_Height_Zero ()
105 using (Bitmap bmp = new Bitmap (10, 10)) {
106 Assert.Throws<OutOfMemoryException> (() => bmp.GetThumbnailImage (5, 0, new Image.GetThumbnailImageAbort (CallbackFalse), IntPtr.Zero));
111 public void GetThumbnailImage_Width_Negative ()
113 using (Bitmap bmp = new Bitmap (10, 10)) {
114 Assert.Throws<OutOfMemoryException> (() => bmp.GetThumbnailImage (-5, 5, new Image.GetThumbnailImageAbort (CallbackFalse), IntPtr.Zero));
119 public void GetThumbnailImage_CallbackData_Invalid ()
121 using (Bitmap bmp = new Bitmap (10, 10)) {
122 // according to documentation IntPtr.Zero must be supplied as data
123 Image tn = bmp.GetThumbnailImage (5, 5, new Image.GetThumbnailImageAbort (CallbackFalse), (IntPtr)Int32.MaxValue);
124 Assert.AreEqual (5, tn.Height, "Height");
125 Assert.AreEqual (5, tn.Width, "Width");
126 Assert.IsFalse (callback, "Callback called");
127 tn.Save (fname, ImageFormat.Tiff);
132 public void GetThumbnailImage_SameSize_Bmp ()
134 using (Bitmap bmp = new Bitmap (10, 10)) {
135 Image tn = bmp.GetThumbnailImage (10, 10, new Image.GetThumbnailImageAbort (CallbackFalse), IntPtr.Zero);
136 Assert.AreEqual (10, tn.Height, "Height");
137 Assert.AreEqual (10, tn.Width, "Width");
138 Assert.IsFalse (callback, "Callback called");
139 tn.Save (fname, ImageFormat.Bmp);
144 public void GetThumbnailImage_Smaller_Gif ()
146 using (Bitmap bmp = new Bitmap (10, 10)) {
147 Image tn = bmp.GetThumbnailImage (4, 4, new Image.GetThumbnailImageAbort (CallbackTrue), IntPtr.Zero);
148 Assert.AreEqual (4, tn.Height, "Height");
149 Assert.AreEqual (4, tn.Width, "Width");
150 Assert.IsFalse (callback, "Callback called");
151 tn.Save (fname, ImageFormat.Gif);
156 public void GetThumbnailImage_Bigger_Png ()
158 using (Bitmap bmp = new Bitmap (10, 10)) {
159 Image tn = bmp.GetThumbnailImage (40, 40, new Image.GetThumbnailImageAbort (CallbackTrue), IntPtr.Zero);
160 Assert.AreEqual (40, tn.Height, "Height");
161 Assert.AreEqual (40, tn.Width, "Width");
162 Assert.IsFalse (callback, "Callback called");
163 tn.Save (fname, ImageFormat.Png);
168 public void Stream_Unlocked ()
172 using (MemoryStream ms = new MemoryStream ()) {
173 using (Bitmap bmp = new Bitmap (10, 10)) {
174 bmp.Save (ms, ImageFormat.Png);
177 img = Image.FromStream (ms);
179 // stream isn't available anymore
180 ((Bitmap) img).MakeTransparent (Color.Transparent);
182 catch (OutOfMemoryException) {
183 int p = (int) Environment.OSVersion.Platform;
184 // libgdiplus (UNIX) doesn't lazy load the image so the
185 // stream may be freed (and this exception will never occur)
186 if ((p == 4) || (p == 128) || (p == 6))
192 public void Stream_Locked ()
195 using (MemoryStream ms = new MemoryStream ()) {
196 using (Bitmap bmp = new Bitmap (10, 10)) {
197 bmp.Save (ms, ImageFormat.Png);
200 img = Image.FromStream (ms);
201 // stream is available
202 ((Bitmap) img).MakeTransparent (Color.Transparent);
207 [Category ("NotWorking")] // http://bugzilla.ximian.com/show_bug.cgi?id=80558
208 public void XmlSerialize ()
210 new XmlSerializer (typeof (Image));
213 private void Wmf (Image img)
215 Assert.IsFalse (img is Bitmap, "Bitmap");
216 Assert.IsTrue (img is Metafile, "Metafile");
218 Assert.AreEqual (327683, img.Flags, "Flags");
219 Assert.IsTrue (img.RawFormat.Equals (ImageFormat.Wmf), "Wmf");
220 Assert.IsNull (img.Tag, "Tag");
224 public void FromFile_Metafile_Wmf ()
226 string filename = TestBitmap.getInFile ("bitmaps/telescope_01.wmf");
227 using (Image img = Image.FromFile (filename)) {
233 public void FromStream_Metafile_Wmf ()
235 string filename = TestBitmap.getInFile ("bitmaps/telescope_01.wmf");
236 using (FileStream fs = File.OpenRead (filename)) {
237 using (Image img = Image.FromStream (fs)) {
244 [Category ("NotWorking")] // https://bugzilla.novell.com/show_bug.cgi?id=338779
245 public void FromStream_Metafile_Wmf_NotOrigin ()
247 string filename = TestBitmap.getInFile ("bitmaps/telescope_01.wmf");
248 using (FileStream fs = File.OpenRead (filename)) {
249 fs.Position = fs.Length / 2;
250 Assert.Throws<ArgumentException> (() => Image.FromStream (fs));
254 private void Emf (Image img)
256 Assert.IsFalse (img is Bitmap, "Bitmap");
257 Assert.IsTrue (img is Metafile, "Metafile");
259 Assert.AreEqual (327683, img.Flags, "Flags");
260 Assert.IsTrue (img.RawFormat.Equals (ImageFormat.Emf), "Emf");
261 Assert.IsNull (img.Tag, "Tag");
265 public void FromFile_Metafile_Emf ()
267 string filename = TestBitmap.getInFile ("bitmaps/milkmateya01.emf");
268 using (Image img = Image.FromFile (filename)) {
274 public void FromStream_Metafile_Emf ()
276 string filename = TestBitmap.getInFile ("bitmaps/milkmateya01.emf");
277 using (FileStream fs = File.OpenRead (filename)) {
278 using (Image img = Image.FromStream (fs)) {
285 [Category ("NotWorking")] // https://bugzilla.novell.com/show_bug.cgi?id=338779
286 public void FromStream_Metafile_Emf_NotOrigin ()
288 string filename = TestBitmap.getInFile ("bitmaps/milkmateya01.emf");
289 using (FileStream fs = File.OpenRead (filename)) {
290 fs.Position = fs.Length / 2;
291 Assert.Throws<ArgumentException> (() => Image.FromStream (fs));
296 public void FromFile_Invalid ()
298 string filename = Assembly.GetExecutingAssembly ().Location;
299 Assert.Throws<OutOfMemoryException> (() => Image.FromFile (filename));
303 public void FromStream_Invalid ()
305 string filename = Assembly.GetExecutingAssembly ().Location;
306 using (FileStream fs = File.OpenRead (filename)) {
307 Assert.Throws<ArgumentException> (() => Image.FromStream (fs));
311 private Bitmap GetBitmap ()
313 Bitmap bmp = new Bitmap (20, 10, PixelFormat.Format24bppRgb);
314 using (Graphics g = Graphics.FromImage (bmp))
316 Pen pen = new Pen (Color.Black, 3);
317 g.DrawRectangle (pen, 0, 0, 5, 10);
323 public void StreamSaveLoad ()
325 using (MemoryStream ms = new MemoryStream ()) {
326 using (Bitmap bmp = GetBitmap ()) {
327 Assert.AreEqual (0, ms.Position, "Position-1");
328 bmp.Save (ms, ImageFormat.Bmp);
329 Assert.IsTrue (ms.Position > 0, "Position-2");
331 ms.Position = ms.Length;
332 Assert.AreEqual (ms.Length, ms.Position, "Position-3");
334 Bitmap bmp2 = (Bitmap)Image.FromStream (ms);
335 Assert.IsTrue (ms.Position > 20, "Position-4");
337 Assert.IsTrue (bmp2.RawFormat.Equals (ImageFormat.Bmp), "Bmp");
339 Assert.AreEqual (bmp.GetPixel (0, 0), bmp2.GetPixel (0, 0), "0,0");
340 Assert.AreEqual (bmp.GetPixel (10, 0), bmp2.GetPixel (10, 0), "10,0");
346 public void StreamJunkSaveLoad ()
348 using (MemoryStream ms = new MemoryStream ()) {
352 Assert.AreEqual (2, ms.Position, "Position-1");
354 using (Bitmap bmp = GetBitmap ()) {
355 bmp.Save (ms, ImageFormat.Bmp);
356 Assert.IsTrue (ms.Position > 2, "Position-2");
358 Assert.Throws<ArgumentException> (() => Image.FromStream (ms));
364 public void XmlSerialization ()
366 new XmlSerializer (typeof (Image));