static public BitmapSource getThumbnail(string filename, int maxHeight) { try { Bitmap bmp = MediaBitmap.fromFile(filename); Bitmap tn = MediaBitmap.ResizeHeight(bmp, maxHeight); BitmapSource image = MediaBitmap.toBitmapSource(tn); tn.Dispose(); return(image); } catch (Exception) { return(null); } }
static public Bitmap extractRectangle(Bitmap bitmap, Point p1, Point p2, Point p3, Point p4) { try { List <Point> pts = new List <Point>(); pts.Add(p1); pts.Add(p2); pts.Add(p3); pts.Add(p4); Point min = new Point(999999, 999999); Point max = new Point(0, 0); foreach (Point point in pts) { if (point.X < min.X) { min.X = (int)point.X; } if (point.Y < min.Y) { min.Y = (int)point.Y; } if (point.X > max.X) { max.X = (int)point.X; } if (point.Y > max.Y) { max.Y = (int)point.Y; } } Bitmap cropped = bitmap.Clone(new Rectangle(min.X, min.Y, max.X - min.X, max.Y - min.Y), bitmap.PixelFormat); double angle1 = (3600 + MathTools.getAngle(pts[0], pts[1])) % 360; double angle2 = (3600 + MathTools.getAngle(pts[1], pts[2]) - 90) % 360; double angle3 = (3600 + MathTools.getAngle(pts[2], pts[3]) - 180) % 360; double angle4 = (3600 + MathTools.getAngle(pts[3], pts[0]) - 270) % 360; double angle = (angle1 + angle2 + angle3 + angle4) / 4; if (Math.Abs(angle2 - angle1) > 5) { angle = angle1; } else if (Math.Abs(angle3 - angle1) > 5) { angle = angle1; } else if (Math.Abs(angle4 - angle1) > 5) { angle = angle1; } int length1 = (int)MathTools.getLength(pts[0], pts[1]); int length2 = (int)MathTools.getLength(pts[1], pts[2]); int length3 = (int)MathTools.getLength(pts[2], pts[3]); int length4 = (int)MathTools.getLength(pts[3], pts[0]); int lengthW = (length1 + length3) / 2; int lengthH = (length2 + length4) / 2; if ((double)Math.Abs(length1 - length3) > 0.05 * (double)Math.Max(length1, length3)) { lengthW = Math.Max(length1, length3); } if ((double)Math.Abs(length2 - length4) > 0.05 * (double)Math.Max(length2, length4)) { lengthH = Math.Max(length2, length4); } Bitmap rotated = MediaBitmap.rotate(cropped, -(float)angle, true); cropped.Dispose(); Rectangle rect = new Rectangle((rotated.Width - lengthW) / 2, (rotated.Height - lengthH) / 2, lengthW, lengthH); if (rect.X < 0 || rect.Y < 0 || rect.Width > rotated.Width || rect.Height > rotated.Height || rect.X + rect.Width > rotated.Width || rect.Y + rect.Height > rotated.Height) { return(rotated); } cropped = rotated.Clone(rect, rotated.PixelFormat); rotated.Dispose(); return(cropped); } catch (Exception) { return(null); } }
static public BitmapSource fromBitmap(Bitmap bitmap) { return(MediaBitmap.toBitmapSource(bitmap)); }