1 | using System;
|
---|
2 | using System.IO;
|
---|
3 | using System.Drawing;
|
---|
4 | using System.Drawing.Imaging;
|
---|
5 |
|
---|
6 | namespace AllocsFixes
|
---|
7 | {
|
---|
8 | public static class PetesUtils {
|
---|
9 | public static bool ValidText(object oText) {
|
---|
10 | if (oText == null)
|
---|
11 | return false;
|
---|
12 |
|
---|
13 | if (oText.GetType () == typeof (string)) {
|
---|
14 | if (((string)oText).Trim ().Length < 1)
|
---|
15 | return false;
|
---|
16 |
|
---|
17 | return true;
|
---|
18 | }
|
---|
19 |
|
---|
20 | if (oText.ToString ().Trim ().Length < 1)
|
---|
21 | return false;
|
---|
22 |
|
---|
23 | return true;
|
---|
24 | }
|
---|
25 |
|
---|
26 | // Note: don't forget to dispose the image (which also disposes the underlying stream) when done with the image.
|
---|
27 | public static System.Drawing.Image GetImageFromBytes(byte[] bImage) {
|
---|
28 | if ((bImage == null) || (bImage.Length == 0))
|
---|
29 | return null;
|
---|
30 |
|
---|
31 | try {
|
---|
32 | MemoryStream ms = new MemoryStream ();
|
---|
33 | ms.Write (bImage, 0, bImage.Length);
|
---|
34 | ms.Seek (0, SeekOrigin.Begin);
|
---|
35 | Image i = Image.FromStream (ms);
|
---|
36 | return i;
|
---|
37 | }
|
---|
38 | catch { }
|
---|
39 |
|
---|
40 | return null;
|
---|
41 | }
|
---|
42 |
|
---|
43 | /*
|
---|
44 | public static System.Drawing.Color BlendColors(System.Drawing.Color color, System.Drawing.Color backColor, double amount) {
|
---|
45 | byte r = (byte)((color.R * amount) + backColor.R * (1 - amount));
|
---|
46 | byte g = (byte)((color.G * amount) + backColor.G * (1 - amount));
|
---|
47 | byte b = (byte)((color.B * amount) + backColor.B * (1 - amount));
|
---|
48 | return System.Drawing.Color.FromArgb (r, g, b);
|
---|
49 | }
|
---|
50 | */
|
---|
51 |
|
---|
52 | /*
|
---|
53 | public static System.Drawing.Color FromAhsb (int alpha, float hue, float saturation, float brightness)
|
---|
54 | {
|
---|
55 | if (0 > alpha || 255 < alpha) {
|
---|
56 | throw new ArgumentOutOfRangeException ("alpha", alpha, "Value must be within a range of 0 - 255.");
|
---|
57 | }
|
---|
58 |
|
---|
59 | if (0f > hue || 360f < hue) {
|
---|
60 | throw new ArgumentOutOfRangeException ("hue", hue, "Value must be within a range of 0 - 360.");
|
---|
61 | }
|
---|
62 |
|
---|
63 | if (0f > saturation || 1f < saturation) {
|
---|
64 | throw new ArgumentOutOfRangeException ("saturation", saturation, "Value must be within a range of 0 - 1.");
|
---|
65 | }
|
---|
66 |
|
---|
67 | if (0f > brightness || 1f < brightness) {
|
---|
68 | throw new ArgumentOutOfRangeException ("brightness", brightness, "Value must be within a range of 0 - 1.");
|
---|
69 | }
|
---|
70 |
|
---|
71 | if (0 == saturation) {
|
---|
72 | return System.Drawing.Color.FromArgb (alpha, Convert.ToInt32 (brightness * 255), Convert.ToInt32 (brightness * 255), Convert.ToInt32 (brightness * 255));
|
---|
73 | }
|
---|
74 |
|
---|
75 | float fMax, fMid, fMin;
|
---|
76 | int iSextant, iMax, iMid, iMin;
|
---|
77 |
|
---|
78 | if (0.5 < brightness) {
|
---|
79 | fMax = brightness - (brightness * saturation) + saturation;
|
---|
80 | fMin = brightness + (brightness * saturation) - saturation;
|
---|
81 | }
|
---|
82 | else {
|
---|
83 | fMax = brightness + (brightness * saturation);
|
---|
84 | fMin = brightness - (brightness * saturation);
|
---|
85 | }
|
---|
86 |
|
---|
87 | iSextant = (int)Math.Floor (hue / 60f);
|
---|
88 | if (300f <= hue)
|
---|
89 | hue -= 360f;
|
---|
90 |
|
---|
91 | hue /= 60f;
|
---|
92 | hue -= 2f * (float)Math.Floor (((iSextant + 1f) % 6f) / 2f);
|
---|
93 |
|
---|
94 | if (0 == iSextant % 2)
|
---|
95 | fMid = hue * (fMax - fMin) + fMin;
|
---|
96 | else
|
---|
97 | fMid = fMin - hue * (fMax - fMin);
|
---|
98 |
|
---|
99 | iMax = Convert.ToInt32 (fMax * 255);
|
---|
100 | iMid = Convert.ToInt32 (fMid * 255);
|
---|
101 | iMin = Convert.ToInt32 (fMin * 255);
|
---|
102 |
|
---|
103 | switch (iSextant) {
|
---|
104 | case 1:
|
---|
105 | return System.Drawing.Color.FromArgb (alpha, iMid, iMax, iMin);
|
---|
106 | case 2:
|
---|
107 | return System.Drawing.Color.FromArgb (alpha, iMin, iMax, iMid);
|
---|
108 | case 3:
|
---|
109 | return System.Drawing.Color.FromArgb (alpha, iMin, iMid, iMax);
|
---|
110 | case 4:
|
---|
111 | return System.Drawing.Color.FromArgb (alpha, iMid, iMin, iMax);
|
---|
112 | case 5:
|
---|
113 | return System.Drawing.Color.FromArgb (alpha, iMax, iMin, iMid);
|
---|
114 | default:
|
---|
115 | return System.Drawing.Color.FromArgb (alpha, iMax, iMid, iMin);
|
---|
116 | }
|
---|
117 | }
|
---|
118 | */
|
---|
119 |
|
---|
120 | public static ImageCodecInfo GetEncoderInfo (String mimeType) {
|
---|
121 | ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders ();
|
---|
122 |
|
---|
123 | foreach (ImageCodecInfo t in encoders)
|
---|
124 | if (t.MimeType == mimeType)
|
---|
125 | return t;
|
---|
126 |
|
---|
127 | return null;
|
---|
128 | }
|
---|
129 |
|
---|
130 | public static byte[] SaveImage_ToBytes (System.Drawing.Image iImage, bool bAndDispose = false)
|
---|
131 | {
|
---|
132 | try {
|
---|
133 | ImageCodecInfo codec = PetesUtils.GetEncoderInfo ("image/png");
|
---|
134 |
|
---|
135 | Encoder qualityEncoder = Encoder.Quality;
|
---|
136 | EncoderParameter ratio = new EncoderParameter (qualityEncoder, 100L);
|
---|
137 | EncoderParameters codecParams = new EncoderParameters (1);
|
---|
138 | codecParams.Param [0] = ratio;
|
---|
139 |
|
---|
140 | byte[] b = null;
|
---|
141 |
|
---|
142 | using (MemoryStream ms = new MemoryStream ()) {
|
---|
143 | iImage.Save (ms, codec, codecParams);
|
---|
144 |
|
---|
145 | b = ms.ToArray ();
|
---|
146 |
|
---|
147 | if (bAndDispose)
|
---|
148 | iImage.Dispose ();
|
---|
149 | }
|
---|
150 |
|
---|
151 | return b;
|
---|
152 | }
|
---|
153 | catch { }
|
---|
154 |
|
---|
155 | return null;
|
---|
156 | }
|
---|
157 |
|
---|
158 | }
|
---|
159 | }
|
---|