Features.ImageInfo.croppedSize C# (CSharp) 메소드

croppedSize() 공개 메소드

finds the propper width and height that will save the image proportion and will have an area <= MAX_IMAGE_AREA
public croppedSize ( System.Drawing.Size origSize ) : System.Drawing.Size
origSize System.Drawing.Size
리턴 System.Drawing.Size
        public Size croppedSize(Size origSize)
        {
            int origSizeArea = origSize.Width*origSize.Height;
            // if the orig area is smaller than PROC_IMAGE_AREA
            if (origSizeArea < MAX_IMAGE_AREA) {
                return origSize;
            }

            Size newSize = new Size();
            float imageRatio = (float)origSize.Width/origSize.Height;
            newSize.Height = (int)Math.Sqrt(MAX_IMAGE_AREA / imageRatio);
            newSize.Height -= newSize.Height % 2;
            newSize.Width = (int)(newSize.Height * imageRatio);
            newSize.Width -= newSize.Width % 2;
            return newSize;
        }

Usage Example

예제 #1
0
 public void TestCroppedSize()
 {
     ImageInfo imInf = new ImageInfo(PATH_160_120_RGB);
     // test when size < PROC_IMAGE_SIZE
     Size testSize = new Size(160, 120);
     Size newSize = imInf.croppedSize(testSize);
     Size wantedSize = new Size(160, 120);
     Assert.AreEqual(wantedSize,newSize);
     Console.WriteLine("wantedArea: {0}, newArea: {1}, newSize: {2}, origSize: {3}",
         wantedSize.Height*wantedSize.Width,newSize.Height*newSize.Width, newSize, testSize);
     // test when size > PROC_IMAGE_SIZE
     testSize = new Size(640, 480);
     newSize = imInf.croppedSize(testSize);
     wantedSize = new Size(320, 240);
     Console.WriteLine("wantedArea: {0}, newArea: {1}, newSize: {2}, origSize: {3}",
         wantedSize.Height * wantedSize.Width, newSize.Height * newSize.Width, newSize, testSize);
     Assert.IsTrue(wantedSize.Height*wantedSize.Width >= newSize.Height*newSize.Width);
     testSize = new Size(1920, 1080);
     newSize = imInf.croppedSize(testSize);
     wantedSize = new Size(320, 240);
     Console.WriteLine("wantedArea: {0}, newArea: {1}, newSize: {2}, origSize: {3}",
         wantedSize.Height * wantedSize.Width, newSize.Height * newSize.Width, newSize, testSize);
     Assert.IsTrue(wantedSize.Height * wantedSize.Width >= newSize.Height * newSize.Width);
     testSize = new Size(1600, 900);
     newSize = imInf.croppedSize(testSize);
     wantedSize = new Size(320, 240);
     Console.WriteLine("wantedArea: {0}, newArea: {1}, newSize: {2}, origSize: {3}",
         wantedSize.Height * wantedSize.Width, newSize.Height * newSize.Width, newSize, testSize);
     Assert.IsTrue(wantedSize.Height * wantedSize.Width >= newSize.Height * newSize.Width);
 }