Crop an image by mouse draging shape. not in rectangle shape.
If I am click and drag the mouse in a particular shape crop image in that shape
Your final image can obviously only be a rectangle and have a height and width.
You would need to keep coordinates of the shape you "draw/drag" and then somehow use this as a mask to set the pixels outside this area to a specific "empty/background" colour or transparent if the image type supports it.
Does this answer your question?
this might help you:
Image img = Imager.Crop(sourceImage, new Rectangle(x, y, width, height));
http://imager.codeplex.com/
instead of new Rectangle you do something else
Related
I am creating an Circle on a bitmap but want to have a hole in it. After serching for half an hour I only found ways to crop an image to a circle. The hard thing is, that the hole in the middle should be transparent as the rest of the Image.
This is the base image and the yellow circle represents the transparent area that should be added.
Thanks for any kind of help.
The start is simple: Create a transparent bitmap by doing a g.Clear(Color.Transparent) and then draw/fill a circle in a color.
The next step is a bit trickier: You next want to paint the hole with transparency.
To do so you need to switch the Graphics object to the right CompositingMode; default is SourceOver but you want SourceCopy. The former overlays the alpha values creating mixed colors. The latter will do what we want: Draw the hole by copying the drawn colors including alpha right over the old ones..
Here is an example:
Bitmap bmp = new Bitmap(500, 500);
using (Graphics g = Graphics.FromImage(bmp))
{
g.Clear(Color.Transparent);
//g.SmoothingMode = SmoothingMode.AntiAlias;
g.CompositingMode = CompositingMode.SourceCopy;
g.FillEllipse(Brushes.DarkGreen, 100, 100, 300, 300);
g.FillEllipse(Brushes.Transparent, 200, 200, 100, 100);
}
pictureBox1.Image = bmp;
This is what is looks like in a PictureBox with a BackgroundImage:
A few notes:
You can also use a semi-transparent brush to create a 'tinted' hole; do not use anti-aliasing for this though, as it would introduce colored fringes.
We used simple circles here but with a GraphicsPath you can create and fill shapes of almost any shape and complexity..
And using a GraphicsPath would also have been an alternative to filling with transparency: By first adding the large and then the smaller, inner ellipse the path would have been created with a hole and filling it would have had the very same result! But I found the solution above more instructive..
Final note: As clarkitect noted, to save, do use a format that supports transparency. Png is always recommended..
I would like to replicate the following cursor:
What I need is to draw that small red square where the pointer is everytime I move the mouse. This is a picturebox control by the way.
What would be the best way to replicate this square?
So, with the help of #CBinet I've been able to do this "pointer square". I've put the code in the Paint event of the picturebox, and in the MouseMove event I store the current point of the mouse and do the picturebox.Invalidate method.
However, now I need to place my cursor created from a file like in the first screenshot, in the bottom of the square. At this moment I have this:
As I said, I need to place the cursor in the right bottom corner of the square so it can be like the first screenshot.
What would be the best solution?
Using System.Windows.Forms.Cursor.Position, you can get the position of the cursor on the screen. Then you can draw a rectangle at that position with an arbitrary size then offset the rectangle by minus half of his size.
int x = Cursor.Position.X;
int y = Cursor.Position.Y;
int size = 10; // Arbitrary size
System.Drawing.Graphics graphics = CreateGraphics();
System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(x - (size / 2), y - (size / 2), size, size);
graphics.DrawRectangle(System.Drawing.Pens.Red, rectangle);
Note that you might have to add a reference to System.Drawing in the current project.
More informations :
Cursor.Position Property
How to: Draw Graphics on a Windows Form
Edit
If you want to position your cursor at the bottom right of the rectangle, all you have to change is the offset of your rectangle :
System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(x - size, y - size, size, size);
I am using "DrawArrowButton" method in my code and I can set the required size of the rectangle enclosing the Arrow but I cannot size the Arrow itself. My requirement is to increase the arrow height or width touch the rectangle side to side.
I am using the below code but I do not know how to resize the arrow.
Graphics g;
Rectangle rectLeftDown = new Rectangle(this.SplitterRectangle.Location, new Size(width, height));
ScrollBarArrowButtonState button1State = ScrollBarArrowButtonState.LeftNormal;
ScrollBarRenderer.DrawArrowButton(g, rectLeftDown, button1State);
Use the Graphics.ScaleTransform Method before drawing.
g.ScaleTransform(2, 2);
Now you can draw in double size (as an example).
I am making and photo editor and displaying an image in pictureBox and I resize the image to fit in the pictureBox without effecting its ratio of sides.
Example if the pictureBox is of size (400x400) and the image is of (800x600) i will resize it to (400x300) programatically.
The problem is I want this image to be the size of (400x400) to cover complete pictureBox for this I want to add transparent part in my image to make it from (400x300) to (400x400)
Snapshot of my image on the pictureBox
In the image above my blue is my image and other part is remaining picturebox.
Again I want image'size (blue one) to be equal to picturebox's size and the remaining part of picturebox is covered by transparent part of picture
Note: I don't want to stretch my image but add transparent part
thanks,
The most direct way is to create the larger bitmap and then DrawImage the picture into it.
Bitmap original = (Bitmap) Bitmap.FromFile(someFileName);
Size sz = yourPictureBox.ClientSize;
Bitmap bmp = new Bitmap(sz.Width, sz.Height);
using (Graphics G = Graphics.FromImage(bmp))
G.DrawImage(original, 0, 0);
yourPictureBox.Image = bmp;
I'm making a program that's cropping images. I have two PictureBoxes and a Button named 'crop'. One picture box contains an image and when I select a rectangle in it and press 'Crop' the selected area appears in the other picture box; so the program is working when I press crop. The problem is: How can I get the image from crop area into picture box Image?
Rectangle rectCropArea;
Image srcImage = null;
TargetPicBox.Refresh();
//Prepare a new Bitmap on which the cropped image will be drawn
Bitmap sourceBitmap = new Bitmap(SrcPicBox.Image, SrcPicBox.Width, SrcPicBox.Height);
Graphics g = TargetPicBox.CreateGraphics();
g.DrawImage(sourceBitmap, new Rectangle(0, 0, TargetPicBox.Width, TargetPicBox.Height),
rectCropArea, GraphicsUnit.Pixel);
//Good practice to dispose the System.Drawing objects when not in use.
sourceBitmap.Dispose();
Image x = TargetPicBox.Image;
The problem is that x = null and the image is showing in the picture box so how can I get the Image from this picture box into the Image variable ?
A couple of issues:
First and most important: You are being confused about the relationship between PictureBox.Image (a Property) and the Graphics you associate with the PictureBox's surface.
The Graphics object you get from Control.CreateGraphics is only able to paint onto the surface of the control; usually not what you want; and even when you do, you usually want to do it in a Paint event using e.Graphics..
So, while your code seems to work, it only paints non-persistent pixels onto the surface. Minimize/maximize and you'll see what non-persistent means..!
To change a Bitmap bmp you need to associate it with a Grahics object like this:
Graphics g = Graphics.FromImage(bmp);
Now you can draw into it:
g.DrawImage(sourceBitmap, targetArea, sourceArea, GraphicsUnit.Pixel);
After that you can assign the Bitmap to the Image Property of the TargetPicBox..
Finally dispose of the Graphics, or better, put it into a using clause..
I am assuming that you have managed to give the rectCropArea meaningful values.
Also note that the way you copy the source bitmap has an error: If you want the full image, do use its Size (*), not the one of the PictureBox!!
And instead of creating a target rectangle, with the same error, simply use the TargetPicBox.ClientRectangle!
Here is an example code for the crop Button:
// a Rectangle for testing
Rectangle rectCropArea = new Rectangle(22,22,55,99);
// see the note below about the aspect ratios of the two rectangles!!
Rectangle targetRect = TargetPicBox.ClientRectangle;
Bitmap targetBitmap = new Bitmap(targetRect.Width, targetRect.Height);
using (Bitmap sourceBitmap = new Bitmap(SrcPicBox.Image,
SrcPicBox.Image.Width, SrcPicBox.Image.Height) )
using (Graphics g = Graphics.FromImage(targetBitmap))
g.DrawImage(sourceBitmap, targetRect, rectCropArea, GraphicsUnit.Pixel);
if (TargetPicBox.Image != null) TargetPicBox.Dispose();
TargetPicBox.Image = targetBitmap;
Of course you should have prepared the Rectangle in the proper mouse events!
Here you would want to decide on the aspect ratio of the result; you probably don't want to distort the result! So you need to decide whether to crop the source cropping rectangle or whether to expand the target rectangle..!
Unless you are sure about the dpi resolution you should use SetResolution to make sure the new image has the same!
Note that since I assign targetBitmap to TargetPicBox.Image I must not dipose of it! Instead, before assigning a new Image, I first Dispose the old one..