File: ImageLoader.cs

package info (click to toggle)
mono 6.8.0.105%2Bdfsg-3.3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,284,512 kB
  • sloc: cs: 11,172,132; xml: 2,850,069; ansic: 671,653; cpp: 122,091; perl: 59,366; javascript: 30,841; asm: 22,168; makefile: 20,093; sh: 15,020; python: 4,827; pascal: 925; sql: 859; sed: 16; php: 1
file content (421 lines) | stat: -rw-r--r-- 14,141 bytes parent folder | download | duplicates (6)
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//-------------------------------------------------------------
// <copyright company=Microsoft Corporation>
//   Copyright  Microsoft Corporation. All Rights Reserved.
// </copyright>
//-------------------------------------------------------------
// @owner=alexgor, deliant
//=================================================================
//  File:		ImageLoader.cs
//
//  Namespace:	System.Web.UI.WebControls[Windows.Forms].Charting.Utilities
//
//	Classes:	ImageLoader
//
//  Purpose:	ImageLoader utility class loads specified image and 
//              caches it in the memory for the future use.
//          
//              Images can be loaded from different places including 
//              Files, URIs, WebRequests and Control Resources.
//
//	Reviewed:	AG - August 7, 2002
//              AG - Microsoft 5, 2007
//
//===================================================================


#region Used Namespaces

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Reflection;
using System.Net;
using System.IO;
using System.Security;
using System.Resources;

#if Microsoft_CONTROL
	using System.Windows.Forms.DataVisualization.Charting;
#else
	using System.Web;
    using System.Web.UI.DataVisualization.Charting;
#endif

#endregion

#if Microsoft_CONTROL
	namespace System.Windows.Forms.DataVisualization.Charting.Utilities
#else
	namespace System.Web.UI.DataVisualization.Charting.Utilities
#endif
{
	/// <summary>
    /// ImageLoader utility class loads and returns specified image 
    /// form the File, URI, Web Request or Chart Resources. 
    /// Loaded images are stored in the internal hashtable which 
    /// allows to improve performance if image need to be used 
    /// several times.
	/// </summary>
	internal class ImageLoader : IDisposable, IServiceProvider
	{
		#region Fields

		// Image storage
		private Hashtable			_imageData = null;

		// Reference to the service container
		private IServiceContainer	_serviceContainer = null;

		#endregion

		#region Constructors and Initialization

		/// <summary>
		/// Default constructor is not accessible.
		/// </summary>
		private ImageLoader()
		{
		}

		/// <summary>
		/// Default public constructor.
		/// </summary>
		/// <param name="container">Service container.</param>
		public ImageLoader(IServiceContainer container)
		{
			if(container == null)
			{
				throw(new ArgumentNullException(SR.ExceptionImageLoaderInvalidServiceContainer));
			}
            _serviceContainer = container;
		}

		/// <summary>
		/// Returns Image Loader service object
		/// </summary>
		/// <param name="serviceType">Requested service type.</param>
		/// <returns>Image Loader service object.</returns>
		[EditorBrowsableAttribute(EditorBrowsableState.Never)]
		object IServiceProvider.GetService(Type serviceType)
		{
			if(serviceType == typeof(ImageLoader))
			{
				return this;
			}
			throw (new ArgumentException( SR.ExceptionImageLoaderUnsupportedType( serviceType.ToString())));
		}

		/// <summary>
		/// Dispose images in the hashtable
		/// </summary>
		public void Dispose()
		{
            if (_imageData != null)
			{
                foreach (DictionaryEntry entry in _imageData)
				{
                    if (entry.Value is IDisposable)
                    {
                        ((IDisposable)entry.Value).Dispose();
                    }
				}
                _imageData = null;
				GC.SuppressFinalize(this);  
			}
		}

		#endregion

		#region Methods

		/// <summary>
		/// Loads image from URL. Checks if image already loaded (cached).
		/// </summary>
        /// <param name="imageURL">Image name (FileName, URL, Resource).</param>
		/// <returns>Image object.</returns>
		public System.Drawing.Image LoadImage(string imageURL)
		{
			return LoadImage(imageURL, true);
		}
			
		/// <summary>
		/// Loads image from URL. Checks if image already loaded (cached).
		/// </summary>
		/// <param name="imageURL">Image name (FileName, URL, Resource).</param>
		/// <param name="saveImage">True if loaded image should be saved in cache.</param>
		/// <returns>Image object</returns>
        public System.Drawing.Image LoadImage(string imageURL, bool saveImage)
		{
            System.Drawing.Image image = null;

			// Check if image is defined in the chart image collection
            if (_serviceContainer != null)
			{
                Chart chart = (Chart)_serviceContainer.GetService(typeof(Chart));
				if(chart != null)
				{
					foreach(NamedImage namedImage in chart.Images)
					{
						if(namedImage.Name == imageURL)
						{
							return namedImage.Image;
						}
					}
				}
			}

			// Create new hashtable
            if (_imageData == null)
			{
                _imageData = new Hashtable(StringComparer.OrdinalIgnoreCase);
			}

			// First check if image with this name already loaded
            if (_imageData.Contains(imageURL))
			{
				image = (System.Drawing.Image)_imageData[imageURL];
			}

#if ! Microsoft_CONTROL

			// Try to load as relative URL using the Control object
			if(image == null)
			{
                Chart control = (Chart)_serviceContainer.GetService(typeof(Chart));
                if (control != null && control.Page != null)
                {
                    if (!control.IsDesignMode())
                    {
                        image = LoadFromFile(control.Page.MapPath(imageURL));
                    }
                    else if (control.IsDesignMode() && !String.IsNullOrEmpty(control.webFormDocumentURL))
                    {   
                        // Find current web page path and fileName
                        Uri pageUri = new Uri(control.webFormDocumentURL);
                        string path = pageUri.LocalPath;
                        string pageFile = pageUri.Segments[pageUri.Segments.Length-1];

                        // Find full image fileName
                        string imageFileRelative = control.ResolveClientUrl(imageURL);
                        string imageFile = path.Replace(pageFile, imageFileRelative);
                        
                        // Load image
                        image = LoadFromFile(imageFile);
                    }
                }

                else if ( HttpContext.Current != null )
                {
                    image = LoadFromFile(HttpContext.Current.Request.MapPath(imageURL));
                }
			}
#endif

			// Try to load image from resource
			if(image == null)
			{
                try
                {

                    // Check if resource class type was specified
                    int columnIndex = imageURL.IndexOf("::", StringComparison.Ordinal);
                    if (columnIndex > 0)
                    {
                        string resourceRootName = imageURL.Substring(0, columnIndex);
                        string resourceName = imageURL.Substring(columnIndex + 2);
                        System.Resources.ResourceManager resourceManager = new System.Resources.ResourceManager(resourceRootName, Assembly.GetExecutingAssembly());
                        image = (System.Drawing.Image)(resourceManager.GetObject(resourceName));
                    }
#if Microsoft_CONTROL
                    else if (Assembly.GetEntryAssembly() != null)
                    {
                        // Check if resource class type was specified
                        columnIndex = imageURL.IndexOf(':');
                        if (columnIndex > 0)
                        {
                            string resourceRootName = imageURL.Substring(0, columnIndex);
                            string resourceName = imageURL.Substring(columnIndex + 1);
                            System.Resources.ResourceManager resourceManager = new System.Resources.ResourceManager(resourceRootName, Assembly.GetEntryAssembly());
                            image = (Image)(resourceManager.GetObject(resourceName));
                        }
                        else
                        {
                            // Try to load resource from every type defined in entry assembly
                            Assembly entryAssembly = Assembly.GetEntryAssembly();
                            if (entryAssembly != null)
                            {
                                foreach (Type type in entryAssembly.GetTypes())
                                {
                                    System.Resources.ResourceManager resourceManager = new System.Resources.ResourceManager(type);
                                    try
                                    {
                                        image = (Image)(resourceManager.GetObject(imageURL));
                                    }
                                    catch (ArgumentNullException)
                                    {
                                    }
                                    catch (MissingManifestResourceException)
                                    {
                                    }

                                    // Check if image was loaded
                                    if (image != null)
                                    {
                                        break;
                                    }
                                }
                            }
                        }
                    }
#endif
                }
                catch (MissingManifestResourceException)
                {
                }
			}
		

			// Try to load image using the Web Request
			if(image == null)
			{
				Uri	imageUri = null;
				try 
				{
					// Try to create URI directly from image URL (will work in case of absolute URL)
					imageUri = new Uri(imageURL);
				}
				catch(UriFormatException)
				{}


				// Load image from file or web resource
				if(imageUri != null)
				{
                    try
                    {
                        WebRequest request = WebRequest.Create(imageUri);
                        image = System.Drawing.Image.FromStream(request.GetResponse().GetResponseStream());
                    }
                    catch (ArgumentException)
                    {
                    }
                    catch (NotSupportedException)
                    {
                    }
                    catch (SecurityException)
                    {
                    }
				}
            }
#if Microsoft_CONTROL
            // absolute uri(without Server.MapPath)in web is not allowed. Loading from replative uri Server[Page].MapPath is done above.
            // Try to load as file
			if(image == null)
			{

                image = LoadFromFile(imageURL);
            }
#endif

            // Error loading image
			if(image == null)
			{
#if ! Microsoft_CONTROL
				throw(new ArgumentException( SR.ExceptionImageLoaderIncorrectImageUrl( imageURL ) ) );
#else
				throw(new ArgumentException( SR.ExceptionImageLoaderIncorrectImageLocation( imageURL ) ) );
#endif
            }

			// Save new image in cache
			if(saveImage)
			{
                _imageData[imageURL] = image;
			}

			return image;
		}

		/// <summary>
		/// Helper function which loads image from file.
		/// </summary>
		/// <param name="fileName">File name.</param>
		/// <returns>Loaded image or null.</returns>
        private System.Drawing.Image LoadFromFile(string fileName)
		{
			// Try to load image from file
			try
			{
				return System.Drawing.Image.FromFile(fileName);
			}
			catch(FileNotFoundException)
			{
				return null;
			}
		}

        /// <summary>
        /// Returns the image size taking the image DPI into consideration.
        /// </summary>
        /// <param name="name">Image name (FileName, URL, Resource).</param>
        /// <param name="graphics">Graphics used to calculate the image size.</param>
        /// <param name="size">Calculated size.</param>
        /// <returns>false if it fails to calculate the size, otherwise true.</returns>
        internal bool GetAdjustedImageSize(string name, Graphics graphics, ref SizeF size)
        {
            Image image = LoadImage(name);

            if (image == null)
                return false;

            GetAdjustedImageSize(image, graphics, ref size);

            return true;
        }

        /// <summary>
        /// Returns the image size taking the image DPI into consideration.
        /// </summary>
        /// <param name="image">Image for whcih to calculate the size.</param>
        /// <param name="graphics">Graphics used to calculate the image size.</param>
        /// <param name="size">Calculated size.</param>
        internal static void GetAdjustedImageSize(Image image, Graphics graphics, ref SizeF size)
        {
            if (graphics != null)
            {
                //this will work in case the image DPI is specified, otherwise the image DPI will be assumed to be same as the screen DPI
                size.Width = image.Width * graphics.DpiX / image.HorizontalResolution;
                size.Height = image.Height * graphics.DpiY / image.VerticalResolution;
            }
            else
            {
                size.Width = image.Width;
                size.Height = image.Height;
            }
        }

        /// <summary>
        /// Checks if the image has the same DPI as the graphics object.
        /// </summary>
        /// <param name="image">Image to be checked.</param>
        /// <param name="graphics">Graphics object to be used.</param>
        /// <returns>true if they match, otherwise false.</returns>
        internal static bool DoDpisMatch(Image image, Graphics graphics)
        {
            return graphics.DpiX == image.HorizontalResolution && graphics.DpiY == image.VerticalResolution;
        }

        internal static Image GetScaledImage(Image image, Graphics graphics)
        {
            Bitmap scaledImage = new Bitmap(image, new Size((int)(image.Width * graphics.DpiX / image.HorizontalResolution),
                (int)(image.Height * graphics.DpiY / image.VerticalResolution)));

            return scaledImage;
        }


		#endregion
	}
}