_Graphic.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.VisualBasic.PowerPacks;
  6. using System.Drawing.Drawing2D;
  7. using System.Drawing;
  8. using System.IO;
  9. using System.Windows.Forms;
  10. namespace IControls_FireManager
  11. {
  12. // 그래픽 관련 함수는 여기에서 처리한다
  13. public static class _Graphic
  14. {
  15. // 선 시작점
  16. public static void LineShape_Start(LineShape lineshape, int x, int y)
  17. {
  18. try
  19. {
  20. lineshape.X1 = x;
  21. lineshape.Y1 = y;
  22. }
  23. catch
  24. {
  25. }
  26. }
  27. // 선 끝점
  28. public static void LineShape_End(LineShape lineshape, int x, int y)
  29. {
  30. try
  31. {
  32. lineshape.X2 = x;
  33. lineshape.Y2 = y;
  34. }
  35. catch
  36. {
  37. }
  38. }
  39. // 선 스타일
  40. public static void LineShape_BorderStyle(LineShape lineshape, DashStyle dashstyle, Color color, int borderwidth)
  41. {
  42. try
  43. {
  44. lineshape.BorderStyle = dashstyle;
  45. lineshape.BorderColor = color;
  46. lineshape.BorderWidth = borderwidth;
  47. }
  48. catch
  49. {
  50. }
  51. }
  52. // 사각형 스타일
  53. public static void RectangleShape_BorderStyle(RectangleShape rectangleshape, DashStyle dashstyle, Color color, int borderwidth)
  54. {
  55. try
  56. {
  57. rectangleshape.BorderStyle = dashstyle;
  58. rectangleshape.BorderColor = color;
  59. rectangleshape.BorderWidth = borderwidth;
  60. }
  61. catch
  62. {
  63. }
  64. }
  65. // 컨트롤에 사각형 그리기
  66. public static void Rectangle_Create(ShapeContainer shapecontainer, string Key,
  67. DashStyle dashstyle, Color linecolor, Color backcolor, int BorderWidth, int x, int y, int w, int h)
  68. {
  69. try
  70. {
  71. RectangleShape rect = new RectangleShape();
  72. rect.Name = Key;
  73. if (backcolor == Color.Transparent)
  74. {
  75. rect.BackStyle = BackStyle.Transparent;
  76. }
  77. else
  78. {
  79. rect.BackStyle = BackStyle.Opaque;
  80. rect.BackColor = backcolor;
  81. }
  82. rect.Location = new Point(x, y);
  83. rect.Size = new Size(w, h);
  84. rect.BorderStyle = dashstyle;
  85. rect.BorderWidth = BorderWidth;
  86. rect.BorderColor = linecolor;
  87. shapecontainer.Shapes.Add(rect);
  88. }
  89. catch
  90. {
  91. }
  92. }
  93. // 선정보를 추출해서 문자열 리턴 (x;y;w;h)
  94. // 반드시 컨테이너에 4개의 라인정보가 있어야만 한다
  95. public static int[] GetRectangleInfo_ByLineShape(ShapeContainer ShapeLines)
  96. {
  97. int[] result = new int[4];
  98. //int x=0; // x좌표중에 가장 작은값
  99. //int y=0; // y좌표중에 가장 작은값
  100. //int w=0; // x좌표중에 가장 큰값 - x;
  101. //int h=0; // y좌표중에 가장 큰값 - y;
  102. try
  103. {
  104. // 최소값
  105. int min_x = ShapeLines.Width;
  106. int min_y = ShapeLines.Height;
  107. // 최대값
  108. int max_x = 0;
  109. int max_y = 0;
  110. foreach (Shape shape in ShapeLines.Shapes)
  111. {
  112. if (shape.GetType().Name.ToString() == "LineShape")
  113. {
  114. LineShape line = (LineShape)shape;
  115. // 최대값 구하기
  116. if (line.X1 >= max_x) max_x = line.X1;
  117. if (line.X2 >= max_x) max_x = line.X2;
  118. if (line.Y1 >= max_y) max_y = line.Y1;
  119. if (line.Y2 >= max_y) max_y = line.Y2;
  120. // 최소값 구하기
  121. if (line.X1 <= min_x) min_x = line.X1;
  122. if (line.X2 <= min_x) min_x = line.X2;
  123. if (line.Y1 <= min_y) min_y = line.Y1;
  124. if (line.Y2 <= min_y) min_y = line.Y2;
  125. }
  126. }
  127. // x
  128. result[0] = min_x;
  129. // y
  130. result[1] = min_y;
  131. // w
  132. result[2] = max_x - min_x;
  133. //h
  134. result[3] = max_y - min_y;
  135. return result;
  136. }
  137. catch
  138. {
  139. return null;
  140. }
  141. }
  142. // color 를 int
  143. public static int ColorToInt(Color colorValue)
  144. {
  145. int r = colorValue.R;
  146. int g = colorValue.G << 8;
  147. int b = colorValue.B << 16;
  148. return r + g + b;
  149. }
  150. public static Color intToColor(int colorValue)
  151. {
  152. int r = (colorValue) & 0xFF;
  153. int g = (colorValue >> 8) & 0xFF;
  154. int b = (colorValue >> 16) & 0xFF;
  155. return Color.FromArgb(255, r, g, b);
  156. }
  157. // 해당 컨트롤의 마우스 좌표 구하고 영역 칠하기
  158. public static void CurrentMousePoint_FillRectangle(Control control, int w, int h, Color color)
  159. {
  160. System.Drawing.Point ptMouse = Cursor.Position;
  161. Graphics g = control.CreateGraphics();
  162. ControlPaint.FillReversibleRectangle(new Rectangle(Cursor.Position, new Size(w, h)), color);
  163. }
  164. // 이미지를 메모리스트림으로 변경후 데이타 비교
  165. //public static bool Compare_MemoryStream(Image oImage, Image cImage)
  166. //{
  167. // using (var rel = new MemoryStream())
  168. // {
  169. // oImage.Save(rel, System.Drawing.Imaging.ImageFormat.Bmp);
  170. // using (var comp = new MemoryStream())
  171. // {
  172. // cImage.Save(comp, System.Drawing.Imaging.ImageFormat.Bmp);
  173. // if (Compare(rel.ToArray(), comp.ToArray()))
  174. // return true;
  175. // else
  176. // return false;
  177. // }
  178. // }
  179. //}
  180. // 바이트 비교
  181. //public static unsafe bool Compare(byte[] a, byte[] b)
  182. //{
  183. // if (a.Length != b.Length) return false;
  184. // var len = a.Length;
  185. // fixed (byte* ap = a, bp = b)
  186. // {
  187. // long* alp = (long*)ap, blp = (long*)bp;
  188. // for (; len >= 8; len -= 8)
  189. // {
  190. // if (*alp != *blp) return false;
  191. // alp++;
  192. // blp++;
  193. // }
  194. // byte* ap2 = (byte*)alp, bp2 = (byte*)blp;
  195. // for (; len > 0; len--)
  196. // {
  197. // if (*ap2 != *bp2) return false;
  198. // ap2++;
  199. // bp2++;
  200. // }
  201. // }
  202. // return true;
  203. //}
  204. // 픽셀 비교
  205. public static bool Compare_Pixel(Image oImage, Image cImage)
  206. {
  207. Bitmap img1, img2;
  208. string img1_ref, img2_ref;
  209. int count1 = 0, count2 = 0;
  210. bool flag = true;
  211. img1 = new Bitmap(oImage);
  212. img2 = new Bitmap(cImage);
  213. if (img1.Width == img2.Width && img1.Height == img2.Height)
  214. {
  215. for (int i = 0; i < img1.Width; i++)
  216. {
  217. for (int j = 0; j < img1.Height; j++)
  218. {
  219. img1_ref = img1.GetPixel(i, j).ToString();
  220. img2_ref = img2.GetPixel(i, j).ToString();
  221. if (img1_ref != img2_ref)
  222. {
  223. count2++;
  224. flag = false;
  225. break;
  226. }
  227. count1++;
  228. }
  229. }
  230. //if (flag == false)
  231. // MessageBox.Show("Sorry, Images are not same , " + count2 + " wrong pixels found");
  232. //else
  233. // MessageBox.Show(" Images are same , " + count1 + " same pixels found and " + count2 + " wrong pixels found");
  234. return flag;
  235. }
  236. else
  237. return flag;//MessageBox.Show("can not compare this images");
  238. }
  239. // 컨트롤 캡쳐
  240. // 사용법 : this.pictureBox2.Image = Capture_By_Component(this.chart1);
  241. public static Image Capture_By_Component(Control component)
  242. {
  243. try
  244. {
  245. Bitmap memoryImage = null;
  246. // 절대 좌표값
  247. Point real = component.PointToScreen(component.Location);
  248. // 컴포넌트 영역
  249. Rectangle rc = new Rectangle(real.X - component.Location.X, real.Y - component.Location.Y, component.Width, component.Height);
  250. // Create new graphics object using handle to window.
  251. using (Graphics graphics = component.CreateGraphics())
  252. {
  253. // 품질향상 코드
  254. graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
  255. graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
  256. // 비트맵 생성
  257. memoryImage = new Bitmap(rc.Width, rc.Height, graphics);
  258. using (Graphics memoryGrahics = Graphics.FromImage(memoryImage))
  259. {
  260. memoryGrahics.CopyFromScreen(rc.X, rc.Y, 0, 0, rc.Size, CopyPixelOperation.SourceCopy);
  261. }
  262. }
  263. return Image.FromHbitmap(memoryImage.GetHbitmap());
  264. }
  265. catch
  266. {
  267. return null;
  268. }
  269. }
  270. // 바이트 배열을 이미지로 변환
  271. public static Image Get_By_byteArray(byte[] data)
  272. {
  273. // 이미지 있을 경우
  274. if (data != null)
  275. return new Bitmap(new MemoryStream(data));
  276. else
  277. return null;
  278. }
  279. // 이미지를 메모리스트림으로 변환
  280. public static MemoryStream GetStream_For_SaveJpg(IntPtr Source, int Size, int height, int width)
  281. {
  282. MemoryStream pictureStream = new MemoryStream();
  283. // Sampling -3 기준..
  284. int stride = -3 * width;
  285. IntPtr Scan0 = (IntPtr)(((int)Source) + (Size - (3 * width)));
  286. //
  287. // 비트맵 생성
  288. //
  289. // stride:
  290. // 한 스캐닝선의 처음부터 다음 스캐닝선까지의 바이트 오프셋을 지정하는 정수입니다. 일반적으로 이 값은 픽셀 형식의 바이트 수(예: 16비트/픽셀의
  291. // 경우 2)와 비트맵의 너비를 곱한 값입니다. 이 매개 변수에 전달된 값은 4의 배수여야 합니다.
  292. //
  293. // format:
  294. // 새 System.Drawing.Bitmap의 System.Drawing.Imaging.PixelFormat 열거입니다.
  295. //
  296. // scan0:
  297. // 픽셀 데이터가 들어 있는 바이트 배열에 대한 포인터입니다.
  298. Bitmap img = new Bitmap(width, height, stride, System.Drawing.Imaging.PixelFormat.Format24bppRgb, Scan0);
  299. // 스트림 데이터에 저장
  300. img.Save(pictureStream, System.Drawing.Imaging.ImageFormat.Jpeg);
  301. return pictureStream;
  302. }
  303. }
  304. }