using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.VisualBasic.PowerPacks; using System.Drawing.Drawing2D; using System.Drawing; using System.IO; using System.Windows.Forms; namespace IControls_FireManager { // 그래픽 관련 함수는 여기에서 처리한다 public static class _Graphic { // 선 시작점 public static void LineShape_Start(LineShape lineshape, int x, int y) { try { lineshape.X1 = x; lineshape.Y1 = y; } catch { } } // 선 끝점 public static void LineShape_End(LineShape lineshape, int x, int y) { try { lineshape.X2 = x; lineshape.Y2 = y; } catch { } } // 선 스타일 public static void LineShape_BorderStyle(LineShape lineshape, DashStyle dashstyle, Color color, int borderwidth) { try { lineshape.BorderStyle = dashstyle; lineshape.BorderColor = color; lineshape.BorderWidth = borderwidth; } catch { } } // 사각형 스타일 public static void RectangleShape_BorderStyle(RectangleShape rectangleshape, DashStyle dashstyle, Color color, int borderwidth) { try { rectangleshape.BorderStyle = dashstyle; rectangleshape.BorderColor = color; rectangleshape.BorderWidth = borderwidth; } catch { } } // 컨트롤에 사각형 그리기 public static void Rectangle_Create(ShapeContainer shapecontainer, string Key, DashStyle dashstyle, Color linecolor, Color backcolor, int BorderWidth, int x, int y, int w, int h) { try { RectangleShape rect = new RectangleShape(); rect.Name = Key; if (backcolor == Color.Transparent) { rect.BackStyle = BackStyle.Transparent; } else { rect.BackStyle = BackStyle.Opaque; rect.BackColor = backcolor; } rect.Location = new Point(x, y); rect.Size = new Size(w, h); rect.BorderStyle = dashstyle; rect.BorderWidth = BorderWidth; rect.BorderColor = linecolor; shapecontainer.Shapes.Add(rect); } catch { } } // 선정보를 추출해서 문자열 리턴 (x;y;w;h) // 반드시 컨테이너에 4개의 라인정보가 있어야만 한다 public static int[] GetRectangleInfo_ByLineShape(ShapeContainer ShapeLines) { int[] result = new int[4]; //int x=0; // x좌표중에 가장 작은값 //int y=0; // y좌표중에 가장 작은값 //int w=0; // x좌표중에 가장 큰값 - x; //int h=0; // y좌표중에 가장 큰값 - y; try { // 최소값 int min_x = ShapeLines.Width; int min_y = ShapeLines.Height; // 최대값 int max_x = 0; int max_y = 0; foreach (Shape shape in ShapeLines.Shapes) { if (shape.GetType().Name.ToString() == "LineShape") { LineShape line = (LineShape)shape; // 최대값 구하기 if (line.X1 >= max_x) max_x = line.X1; if (line.X2 >= max_x) max_x = line.X2; if (line.Y1 >= max_y) max_y = line.Y1; if (line.Y2 >= max_y) max_y = line.Y2; // 최소값 구하기 if (line.X1 <= min_x) min_x = line.X1; if (line.X2 <= min_x) min_x = line.X2; if (line.Y1 <= min_y) min_y = line.Y1; if (line.Y2 <= min_y) min_y = line.Y2; } } // x result[0] = min_x; // y result[1] = min_y; // w result[2] = max_x - min_x; //h result[3] = max_y - min_y; return result; } catch { return null; } } // color 를 int public static int ColorToInt(Color colorValue) { int r = colorValue.R; int g = colorValue.G << 8; int b = colorValue.B << 16; return r + g + b; } public static Color intToColor(int colorValue) { int r = (colorValue) & 0xFF; int g = (colorValue >> 8) & 0xFF; int b = (colorValue >> 16) & 0xFF; return Color.FromArgb(255, r, g, b); } // 해당 컨트롤의 마우스 좌표 구하고 영역 칠하기 public static void CurrentMousePoint_FillRectangle(Control control, int w, int h, Color color) { System.Drawing.Point ptMouse = Cursor.Position; Graphics g = control.CreateGraphics(); ControlPaint.FillReversibleRectangle(new Rectangle(Cursor.Position, new Size(w, h)), color); } // 이미지를 메모리스트림으로 변경후 데이타 비교 //public static bool Compare_MemoryStream(Image oImage, Image cImage) //{ // using (var rel = new MemoryStream()) // { // oImage.Save(rel, System.Drawing.Imaging.ImageFormat.Bmp); // using (var comp = new MemoryStream()) // { // cImage.Save(comp, System.Drawing.Imaging.ImageFormat.Bmp); // if (Compare(rel.ToArray(), comp.ToArray())) // return true; // else // return false; // } // } //} // 바이트 비교 //public static unsafe bool Compare(byte[] a, byte[] b) //{ // if (a.Length != b.Length) return false; // var len = a.Length; // fixed (byte* ap = a, bp = b) // { // long* alp = (long*)ap, blp = (long*)bp; // for (; len >= 8; len -= 8) // { // if (*alp != *blp) return false; // alp++; // blp++; // } // byte* ap2 = (byte*)alp, bp2 = (byte*)blp; // for (; len > 0; len--) // { // if (*ap2 != *bp2) return false; // ap2++; // bp2++; // } // } // return true; //} // 픽셀 비교 public static bool Compare_Pixel(Image oImage, Image cImage) { Bitmap img1, img2; string img1_ref, img2_ref; int count1 = 0, count2 = 0; bool flag = true; img1 = new Bitmap(oImage); img2 = new Bitmap(cImage); if (img1.Width == img2.Width && img1.Height == img2.Height) { for (int i = 0; i < img1.Width; i++) { for (int j = 0; j < img1.Height; j++) { img1_ref = img1.GetPixel(i, j).ToString(); img2_ref = img2.GetPixel(i, j).ToString(); if (img1_ref != img2_ref) { count2++; flag = false; break; } count1++; } } //if (flag == false) // MessageBox.Show("Sorry, Images are not same , " + count2 + " wrong pixels found"); //else // MessageBox.Show(" Images are same , " + count1 + " same pixels found and " + count2 + " wrong pixels found"); return flag; } else return flag;//MessageBox.Show("can not compare this images"); } // 컨트롤 캡쳐 // 사용법 : this.pictureBox2.Image = Capture_By_Component(this.chart1); public static Image Capture_By_Component(Control component) { try { Bitmap memoryImage = null; // 절대 좌표값 Point real = component.PointToScreen(component.Location); // 컴포넌트 영역 Rectangle rc = new Rectangle(real.X - component.Location.X, real.Y - component.Location.Y, component.Width, component.Height); // Create new graphics object using handle to window. using (Graphics graphics = component.CreateGraphics()) { // 품질향상 코드 graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; // 비트맵 생성 memoryImage = new Bitmap(rc.Width, rc.Height, graphics); using (Graphics memoryGrahics = Graphics.FromImage(memoryImage)) { memoryGrahics.CopyFromScreen(rc.X, rc.Y, 0, 0, rc.Size, CopyPixelOperation.SourceCopy); } } return Image.FromHbitmap(memoryImage.GetHbitmap()); } catch { return null; } } // 바이트 배열을 이미지로 변환 public static Image Get_By_byteArray(byte[] data) { // 이미지 있을 경우 if (data != null) return new Bitmap(new MemoryStream(data)); else return null; } // 이미지를 메모리스트림으로 변환 public static MemoryStream GetStream_For_SaveJpg(IntPtr Source, int Size, int height, int width) { MemoryStream pictureStream = new MemoryStream(); // Sampling -3 기준.. int stride = -3 * width; IntPtr Scan0 = (IntPtr)(((int)Source) + (Size - (3 * width))); // // 비트맵 생성 // // stride: // 한 스캐닝선의 처음부터 다음 스캐닝선까지의 바이트 오프셋을 지정하는 정수입니다. 일반적으로 이 값은 픽셀 형식의 바이트 수(예: 16비트/픽셀의 // 경우 2)와 비트맵의 너비를 곱한 값입니다. 이 매개 변수에 전달된 값은 4의 배수여야 합니다. // // format: // 새 System.Drawing.Bitmap의 System.Drawing.Imaging.PixelFormat 열거입니다. // // scan0: // 픽셀 데이터가 들어 있는 바이트 배열에 대한 포인터입니다. Bitmap img = new Bitmap(width, height, stride, System.Drawing.Imaging.PixelFormat.Format24bppRgb, Scan0); // 스트림 데이터에 저장 img.Save(pictureStream, System.Drawing.Imaging.ImageFormat.Jpeg); return pictureStream; } } }