Mercurial > silverbladetech
comparison Chronosv2/source/Extensions/BitmapExtensions.cs @ 10:443821e55f06
Initial cleaned up add from Codeplex files
author | stevenh7776 stevenhollidge@hotmail.com |
---|---|
date | Tue, 21 Feb 2012 17:25:44 +0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
9:904a9faadf8b | 10:443821e55f06 |
---|---|
1 using System; | |
2 using System.Drawing; | |
3 using System.Drawing.Drawing2D; | |
4 | |
5 namespace Chronos.Extensions | |
6 { | |
7 /// <summary> | |
8 /// Extension methods for the System.Drawing.Bitmap class | |
9 /// </summary> | |
10 public static class BitmapExtensions | |
11 { | |
12 #region · Extensions · | |
13 | |
14 /// <summary> | |
15 /// Scales the bitmap to the passed target size without respecting the aspect. | |
16 /// </summary> | |
17 /// <param name="bitmap">The source bitmap.</param> | |
18 /// <param name="size">The target size.</param> | |
19 /// <returns>The scaled bitmap</returns> | |
20 /// <example><code> | |
21 /// var bitmap = new Bitmap("image.png"); | |
22 /// var thumbnail = bitmap.ScaleToSize(100, 100); | |
23 /// </code></example> | |
24 public static Bitmap ScaleToSize(this Bitmap bitmap, Size size) | |
25 { | |
26 return bitmap.ScaleToSize(size.Width, size.Height); | |
27 } | |
28 | |
29 /// <summary> | |
30 /// Scales the bitmap to the passed target size without respecting the aspect. | |
31 /// </summary> | |
32 /// <param name="bitmap">The source bitmap.</param> | |
33 /// <param name="width">The target width.</param> | |
34 /// <param name="height">The target height.</param> | |
35 /// <returns>The scaled bitmap</returns> | |
36 /// <example><code> | |
37 /// var bitmap = new Bitmap("image.png"); | |
38 /// var thumbnail = bitmap.ScaleToSize(100, 100); | |
39 /// </code></example> | |
40 public static Bitmap ScaleToSize(this Bitmap bitmap, int width, int height) | |
41 { | |
42 var scaledBitmap = new Bitmap(width, height); | |
43 | |
44 using (var g = Graphics.FromImage(scaledBitmap)) | |
45 { | |
46 g.InterpolationMode = InterpolationMode.HighQualityBicubic; | |
47 g.DrawImage(bitmap, 0, 0, width, height); | |
48 } | |
49 | |
50 return scaledBitmap; | |
51 } | |
52 | |
53 /// <summary> | |
54 /// Scales the bitmap to the passed target size by respecting the aspect. | |
55 /// </summary> | |
56 /// <param name="bitmap">The source bitmap.</param> | |
57 /// <param name="size">The target size.</param> | |
58 /// <returns>The scaled bitmap</returns> | |
59 /// <example><code> | |
60 /// var bitmap = new Bitmap("image.png"); | |
61 /// var thumbnail = bitmap.ScaleProportional(100, 100); | |
62 /// </code></example> | |
63 /// <remarks>Please keep in mind that the returned bitmaps size might not match the desired size due to original bitmaps aspect.</remarks> | |
64 public static Bitmap ScaleProportional(this Bitmap bitmap, Size size) | |
65 { | |
66 return bitmap.ScaleProportional(size.Width, size.Height); | |
67 } | |
68 | |
69 /// <summary> | |
70 /// Scales the bitmap to the passed target size by respecting the aspect. | |
71 /// </summary> | |
72 /// <param name="bitmap">The source bitmap.</param> | |
73 /// <param name="width">The target width.</param> | |
74 /// <param name="height">The target height.</param> | |
75 /// <returns>The scaled bitmap</returns> | |
76 /// <example><code> | |
77 /// var bitmap = new Bitmap("image.png"); | |
78 /// var thumbnail = bitmap.ScaleProportional(100, 100); | |
79 /// </code></example> | |
80 /// <remarks>Please keep in mind that the returned bitmaps size might not match the desired size due to original bitmaps aspect.</remarks> | |
81 public static Bitmap ScaleProportional(this Bitmap bitmap, int width, int height) | |
82 { | |
83 float proportionalWidth, proportionalHeight; | |
84 | |
85 if (width.Equals(0)) | |
86 { | |
87 proportionalWidth = ((float)height) / bitmap.Size.Height * bitmap.Width; | |
88 proportionalHeight = height; | |
89 } | |
90 else if (height.Equals(0)) | |
91 { | |
92 proportionalWidth = width; | |
93 proportionalHeight = ((float)width) / bitmap.Size.Width * bitmap.Height; | |
94 } | |
95 else if (((float)width) / bitmap.Size.Width * bitmap.Size.Height <= height) | |
96 { | |
97 proportionalWidth = width; | |
98 proportionalHeight = ((float)width) / bitmap.Size.Width * bitmap.Height; | |
99 } | |
100 else | |
101 { | |
102 proportionalWidth = ((float)height) / bitmap.Size.Height * bitmap.Width; | |
103 proportionalHeight = height; | |
104 } | |
105 | |
106 return bitmap.ScaleToSize((int)proportionalWidth, (int)proportionalHeight); | |
107 } | |
108 | |
109 /// <summary> | |
110 /// Scales the bitmap to the passed target size by respecting the aspect. The overlapping background is filled with the given background color. | |
111 /// </summary> | |
112 /// <param name="bitmap">The source bitmap.</param> | |
113 /// <param name="size">The target size.</param> | |
114 /// <returns>The scaled bitmap</returns> | |
115 /// <example><code> | |
116 /// var bitmap = new Bitmap("image.png"); | |
117 /// var thumbnail = bitmap.ScaleToSizeProportional(100, 100); | |
118 /// </code></example> | |
119 public static Bitmap ScaleToSizeProportional(this Bitmap bitmap, Size size) | |
120 { | |
121 return bitmap.ScaleToSizeProportional(Color.White, size); | |
122 } | |
123 | |
124 /// <summary> | |
125 /// Scales the bitmap to the passed target size by respecting the aspect. The overlapping background is filled with the given background color. | |
126 /// </summary> | |
127 /// <param name="bitmap">The source bitmap.</param> | |
128 /// <param name="backgroundColor">The color of the background.</param> | |
129 /// <param name="size">The target size.</param> | |
130 /// <returns>The scaled bitmap</returns> | |
131 /// <example><code> | |
132 /// var bitmap = new Bitmap("image.png"); | |
133 /// var thumbnail = bitmap.ScaleToSizeProportional(100, 100); | |
134 /// </code></example> | |
135 public static Bitmap ScaleToSizeProportional(this Bitmap bitmap, Color backgroundColor, Size size) | |
136 { | |
137 return bitmap.ScaleToSizeProportional(backgroundColor, size.Width, size.Height); | |
138 } | |
139 | |
140 /// <summary> | |
141 /// Scales the bitmap to the passed target size by respecting the aspect. The overlapping background is filled with the given background color. | |
142 /// </summary> | |
143 /// <param name="bitmap">The source bitmap.</param> | |
144 /// <param name="width">The target width.</param> | |
145 /// <param name="height">The target height.</param> | |
146 /// <returns>The scaled bitmap</returns> | |
147 /// <example><code> | |
148 /// var bitmap = new Bitmap("image.png"); | |
149 /// var thumbnail = bitmap.ScaleToSizeProportional(100, 100); | |
150 /// </code></example> | |
151 public static Bitmap ScaleToSizeProportional(this Bitmap bitmap, int width, int height) | |
152 { | |
153 return bitmap.ScaleToSizeProportional(Color.White, width, height); | |
154 } | |
155 | |
156 /// <summary> | |
157 /// Scales the bitmap to the passed target size by respecting the aspect. The overlapping background is filled with the given background color. | |
158 /// </summary> | |
159 /// <param name="bitmap">The source bitmap.</param> | |
160 /// <param name="backgroundColor">The color of the background.</param> | |
161 /// <param name="width">The target width.</param> | |
162 /// <param name="height">The target height.</param> | |
163 /// <returns>The scaled bitmap</returns> | |
164 /// <example><code> | |
165 /// var bitmap = new Bitmap("image.png"); | |
166 /// var thumbnail = bitmap.ScaleToSizeProportional(100, 100); | |
167 /// </code></example> | |
168 public static Bitmap ScaleToSizeProportional(this Bitmap bitmap, Color backgroundColor, int width, int height) | |
169 { | |
170 var scaledBitmap = new Bitmap(width, height); | |
171 using (var g = Graphics.FromImage(scaledBitmap)) | |
172 { | |
173 g.Clear(backgroundColor); | |
174 | |
175 var proportionalBitmap = bitmap.ScaleProportional(width, height); | |
176 | |
177 var imagePosition = new Point | |
178 ( | |
179 (int)((width - proportionalBitmap.Width) / 2m), | |
180 (int)((height - proportionalBitmap.Height) / 2m) | |
181 ); | |
182 | |
183 g.DrawImage(proportionalBitmap, imagePosition); | |
184 } | |
185 | |
186 return scaledBitmap; | |
187 } | |
188 | |
189 #endregion | |
190 } | |
191 } |