I am using croppic to crop images. Is there any possibility to extend its functionality to rotate the image, before sending it to crop. Something like this
var angle = 0;
$('#button').on('click', function() {
angle += 90;
$("#image").rotate(angle);
});
Here is the demo
I can manage to to rotate the image, but the cropped image is not in rotated position. Any idea?
Update:
Now the image is getting rotated. I have managed to keep rotated image by the same handler that uses crop method. But the cropped image in rotated position occurs only when the rotated image is in 180 rotation. When its in the 90/270, the image, obviously don't fit in the bounding box (if you have seen croppic) and I get a MemoryOutOfBound exception.
Here is the code:
public static Bitmap RotateImage(Bitmap bmpSrc, float theta)
{
Matrix mRotate = new Matrix();
mRotate.Translate(bmpSrc.Width / -2, bmpSrc.Height / -2, MatrixOrder.Append);
mRotate.RotateAt(theta, new System.Drawing.Point(0, 0), MatrixOrder.Append);
using (GraphicsPath gp = new GraphicsPath())
{ // transform image points by rotation matrix
gp.AddPolygon(new System.Drawing.Point[] { new System.Drawing.Point(0, 0), new System.Drawing.Point(bmpSrc.Width, 0), new System.Drawing.Point(0, bmpSrc.Height) });
gp.Transform(mRotate);
System.Drawing.PointF[] pts = gp.PathPoints;
// create destination bitmap sized to contain rotated source image
Rectangle bbox = boundingBox(bmpSrc, mRotate);
Bitmap bmpDest = new Bitmap(bbox.Width, bbox.Height);
using (Graphics gDest = Graphics.FromImage(bmpDest))
{ // draw source into dest
Matrix mDest = new Matrix();
mDest.Translate(bmpDest.Width / 2, bmpDest.Height / 2, MatrixOrder.Append);
gDest.Transform = mDest;
gDest.DrawImage(bmpSrc, pts);
return bmpDest;
}
}
}
private static Rectangle boundingBox(Image img, Matrix matrix)
{
GraphicsUnit gu = new GraphicsUnit();
Rectangle rImg = Rectangle.Round(img.GetBounds(ref gu));
// Transform the four points of the image, to get the resized bounding box.
System.Drawing.Point topLeft = new System.Drawing.Point(rImg.Left, rImg.Top);
System.Drawing.Point topRight = new System.Drawing.Point(rImg.Right, rImg.Top);
System.Drawing.Point bottomRight = new System.Drawing.Point(rImg.Right, rImg.Bottom);
System.Drawing.Point bottomLeft = new System.Drawing.Point(rImg.Left, rImg.Bottom);
System.Drawing.Point[] points = new System.Drawing.Point[] { topLeft, topRight, bottomRight, bottomLeft };
GraphicsPath gp = new GraphicsPath(points,new byte[] { (byte)PathPointType.Start,
(byte)PathPointType.Line, (byte)PathPointType.Line,
(byte)PathPointType.Line });
gp.Transform(matrix);
return Rectangle.Round(gp.GetBounds());
}
I think main issue is in the bounding box.
using (Bitmap rotate = new Bitmap(RotateImage(bmp, rotateAngle)))
{
using (Bitmap cloneRotate = (Bitmap)rotate.Clone()) //getting error here
{
//some stuffs
cloneRotate.save(mycroppedimageurl);
}
}
Related
I am trying to create this 2D polygon but I get a blank a image after plotting the points.
The Point class takes x and y axes as parameters. Point(x, y)
`
Bitmap bitmap = new Bitmap(1000, 800, PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
Pen pen = new Pen(System.Drawing.Color.Blue, 2);
Point[] points =
{
new Point(2408, 919),
new Point(2596, 919),
new Point(2596, 1200),
new Point(2408, 919),
new Point(2596, 1505),
new Point(2572, 1505),
new Point(2408, 1200),
new Point(2408, 919)
};
graphics.DrawPolygon(pen, points);
bitmap.Save("DrawPolygon.png");
Update: The polygon now displays as per Jimi's first comment. New issue is the polygon shortest length always sit on the horizontal axis and the polygon lines are crossing.
I am trying to detect Circle inside Rectangle in AForge. I have successfully determined Rectangles but unable to find circles inside Rectangle. How to find shape inside another shape in AForge.
string strPath = Server.MapPath("~/Recipt001.png");
Bitmap myBitmap = new Bitmap(strPath);
//Some filters Grayscale, invert, threshold
//Blod Filtering
BlobCounter blobCounter = new BlobCounter();
blobCounter.ProcessImage(temp);
blobCounter.ObjectsOrder = ObjectsOrder.YX;
blobCounter.FilterBlobs = true;
Blob[] blobs = blobCounter.GetObjectsInformation();
Graphics g = Graphics.FromImage(myBitmap);
Pen redPen = new Pen(Color.Red, 2);
SimpleShapeChecker shapeChecker = new SimpleShapeChecker();
// dictionary of color to highlight different shapes
Dictionary<PolygonSubType, Color> colors = new Dictionary<PolygonSubType, Color>();
colors.Add(PolygonSubType.Unknown, Color.White);
colors.Add(PolygonSubType.Trapezoid, Color.Orange);
colors.Add(PolygonSubType.Parallelogram, Color.Red);
colors.Add(PolygonSubType.Rectangle, Color.Green);
colors.Add(PolygonSubType.Square, Color.Blue);
colors.Add(PolygonSubType.Rhombus, Color.Gray);
colors.Add(PolygonSubType.EquilateralTriangle, Color.Pink);
colors.Add(PolygonSubType.IsoscelesTriangle, Color.Purple);
colors.Add(PolygonSubType.RectangledTriangle, Color.SkyBlue);
colors.Add(PolygonSubType.RectangledIsoscelesTriangle, Color.SeaGreen);
for (int i = 0, n = blobs.Length; i < n; i++)
{
List<IntPoint> corners;
List<IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]);
Point center;
double radius;
if (shapeChecker.IsQuadrilateral(edgePoints, out corners))
{
if (shapeChecker.CheckPolygonSubType(corners) == PolygonSubType.Rectangle)
{
g.DrawPolygon(redPen, ToPointsArray(corners));
}
}
}
redPen.Dispose();
g.Dispose();
None of image processing libraries and even image processing in MATLAB lets you search ROI inside ROI (ROI - region of intrest like rectangles or circles). Concept is CROP REGION -> SEARCH OBJECTS IN REGION
So first locate primary rectangles, thereafter crop image to rectangles and perform circle search inside them. Otherwise search for all circles and all rectangles and then classify circles to be belonging to which rectangle using simple maths.
I have an app that will draw many shapes (rectangle, line and circle) on a panel.
the panel can zoom in and out this shapes.
What I'm trying to do is when the application is fired I need to have the shapes zoomed to fit the window.
How I can do that, I read a lot about defined Images but not shapes.
here is my snap shot
private void panel1_Paint(object sender, PaintEventArgs e)
{
SolidBrush brushs = new SolidBrush(Color.White);
e.Graphics.Clip = new Region(new Rectangle(0, 0, Viewer.Width, Viewer.Height));
e.Graphics.FillRegion(brushs, e.Graphics.Clip);
Graphics g = e.Graphics;
g.TranslateTransform(_ImgX, _ImgY);
g.ScaleTransform(_Zoom, _Zoom);
g.SmoothingMode = SmoothingMode.AntiAlias;
SolidBrush myBrush = new SolidBrush(Color.Black);
Pen p = new Pen(Color.Red);
foreach (CircuitData.ResistorRow resistorRow in ResistorData.Resistor)
{
RectangleF rec = new RectangleF((float)(resistorRow.CenterX - resistorRow.Length/ 2), (float)(resistorRow.CenterY - resistorRow.Width/ 2), (float)resistorRow.Length, (float)resistorRow.Width);
float orientation = 360 - (float)resistorRow.Orientation;
PointF center = new PointF((float)resistorRow.CenterX, (float)resistorRow.CenterY);
PointF[] points = CreatePolygon(rec, center, orientation);
if (!Double.IsNaN(resistorRow.HiX) && !Double.IsNaN(resistorRow.HiY))
{
g.FillEllipse(myBrush, (float)resistorRow.HiX - 2 , (float)resistorRow.HiY - 2, 4, 4);
g.DrawLine(p, new PointF((float)resistorRow.HiX , (float)resistorRow.HiY ), center);
}
g.FillPolygon(myBrush, points);
}
}
Thanks
Draw your shapes onto a Metafile. Its dimensions will be calculated automatically after it is created. At the end you can zoom it safely when you draw it onto the panel.
// the reference Graphics can be taken from your form, its size does not matter
Graphics refGraph = this.CreateGraphics();
IntPtr hdc = refGraph.GetHdc();
Metafile result = new Metafile(hdc, EmfType.EmfOnly, "Shapes");
using (var g = Graphics.FromImage(result))
{
// draw your shapes here (zooming is not necessary)
DrawShapes(g);
}
refGraph.ReleaseHdc(hdc);
refGraph.Dispose();
// use this metafile on the panel
return result;
I have some code that adds text to a bitmap. This works well unless the text is wider than the original image.
How do I go about making the image wide enough for my text to display correctly?
Here is my code:
private BitmapDescriptor GetCustomBitmapDescriptor(string text)
{
Bitmap baseBitmap = BitmapFactory.DecodeResource(Resources, Resource.Drawable.Icon);
Bitmap bitmap = baseBitmap.Copy(Bitmap.Config.Argb8888, true);
Paint paint = new Paint(PaintFlags.AntiAlias);
Rect bounds = new Rect();
paint.GetTextBounds(text, 0, text.Length, bounds);
float bitmapMiddle = bitmap.Width / 2.0f;
Canvas canvas = new Canvas(bitmap);
canvas.DrawText(text, bitmapMiddle - (bounds.Right / 2.0f), bitmap.Height, paint);
BitmapDescriptor icon = BitmapDescriptorFactory.FromBitmap(bitmap);
return (icon);
}
Thanks in advance.
Just color code and it is working
private Bitmap GetCustomBitmapDescriptor(String text)
{
Bitmap baseBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launche);
Bitmap bitmap = baseBitmap.copy(Bitmap.Config.ARGB_8888, true);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
Rect bounds = new Rect();
paint.getTextBounds(text, 0, text.length(), bounds);
float bitmapMiddle = bitmap.getWidth() / 2.0f;
Canvas canvas = new Canvas(bitmap);
paint.setColor(Color.RED);
canvas.drawText(text, bitmapMiddle - (bounds.right / 2.0f), bitmap.getHeight(), paint);
return bitmap;
}
I want to crop a rotated Rectangle from an image. What I want to do is something like that :
System.Windows.Shapes.Rectangle rect = new System.Windows.Shapes.Rectangle();
// rect.RadiusX = ...; // rect.Height = ....; ..
rect.RenderTransform = new RotateTransform(angle);
and then crop this rectangle from Image.
All codes that I have found cropped a System.Windows.Drawing rectangle from the image.But, I need to crop a System.Windows.Shapes to apply the rotate Transform which is not applicable with System.Windows.Drawing Rectangle.
Since your question lacked some code. So as far as I have understood, the given method would provide you with appropriate results. If the answer is not upto your mark, then please elaborate the question statement more.
public static Bitmap CropRotatedRect(Bitmap source, Rectangle rect, float angle, bool HighQuality)
{
Bitmap result = new Bitmap(rect.Width, rect.Height);
using (Graphics g = Graphics.FromImage(result))
{
g.InterpolationMode = HighQuality ? InterpolationMode.HighQualityBicubic : InterpolationMode.Default;
using (Matrix mat = new Matrix())
{
mat.Translate(-rect.Location.X, -rect.Location.Y);
mat.RotateAt(angle, rect.Location);
g.Transform = mat;
g.DrawImage(source, new Point(0, 0));
}
}
return result;
}
Hope that helps.