Using System.Drawing to make a selection tool, and cropping an image in vb.net - vb.net

If i wanted to crop an image in VB.net, how would I go about doing it? I am trying to let the user drag out the box they want (system.drawing.rectangle), and it will automatically remove the edges surrounding the box.
My first problem is primarily the fact that I cannot make the system.drawing.rectangle visible. It is not displaying at all, I am setting its location and height programmatically, but nothing is showing up. I know there is probably something fairly obvious I am missing...but I cannot seem to find it.
My larger issue, however, lies with the cropping itself. I cannot find any crop methods, at all. Is there a hidden one I am missing? Or must I code it myself? How would I go about doing this? It ought to be able to output to a bitmap image object.
Thanks for the help, I am surprised this hasn't been asked on here before....

Regarding your first problem: a Rectangle isn't by itself visible. You have to draw it on your canvas using the Graphics object's DrawRectangle(...) method. For drawing a selection tool, you'll want to set your Pen object's DashCap and DashPattern properties.
To "crop" an image, you basically want to take the portion of a larger image delineated by a smaller Rectangle, and turn it into a new Bitmap. This can be done using one of the 30 overloads of the Graphics object's DrawImage(...) method. You can either keep the cropped portion in its original dimensions (resulting in a smaller Bitmap than your original), or you can "blow it up" to something like the original image's size. If you do the latter approach, it is usually a good idea to set your Graphics object's InterpolationMode property to HighQualityBicubic (I think that's the one, anyway), since the default InterpolationMode is pretty crappy.
There are a number of different ways of rendering images in .Net - it might help if you posted some of your code, along with an explanation of the exact problems you're running into.
Here is another answer with a link to a sample app (and source code in C#, sorry) that may help you get started.

There are a number of articles on these topics on CodeProject:
Pick your favorite flavor (though I encourage you to check out the C# projects - it shouldn't be too hard to convert).
VB
Image Cropping with Image Resizing Using vb.net
C#
Cropping Images
An Easy to Use Image Resizing and Cropping Control
Image Processing using C# (see the Cropping section - I was able to use this code in one of my projects)
WPF/C#
WPF Interactive Image Cropping Control
A Photoshop-like Cropping Adorner for WPF

Related

Rotating a RectangleShape without using Graphics

For a program that I'm making in vb.net, I need to have a rectangle, with an image displayed on it, rotate and move around the screen. It needs to move quickly and responsively, so I'm using the standard RectangleShape. The problem here is that vb.net apparently has no in-built function to rotate this rectangle. I'm not really able to use the corresponding Graphics equivalent with the FillRectangle, as it's incredibly laggy on the computer I'm using for this- since it requires constant DrawImage functions for separate bitmaps.
So, is there a way to have a Rectangle that can:
Hold an image
Be rotated
Be moved around the stage in a very cpu-unintensive manner
Thank you
Dim mxRotate As New Matrix()
'75 being the arbitrary number I picked to rotate by
mxRotate.Rotate(75, MatrixOrder.Append)
e.Graphics.Transform = mxRotate
e.Graphics.DrawRectangle(YourPen, YourRect)
This can probably help you rotate the image: How to rotate JPEG using Graphics.RotateTransform without clipping
As for performance. I'd imagine all of this will use a little bit of the CPU. Alternatively, you can use DirectX or OpenGL for rendering if that's an option.

VB transparent picturebox over several pictureboxes

I am currently writing a program that requires a picture box with a transparent image to go over several pictureboxes. I have looked for hours now and found nothing useful. the program is a rubiks cube solver. each square is represented as a picturebox and the cube is shown in a net form. I cant upload images as my account is new but i have included an image of a rubiks cube in a net form. now imagine a semi transparent image going over some of the squares (pictureboxes) to represent a visual guide to the rotation about to be made in the solve process. If you need more info I will do my best and upload images and add code for better clarification if needed. If this is not possible is there any alternatives that I could attempt? Thankyou
I don't think what you want is possible. I was actually trying to solve the same problem about 2 days ago and to no avail. You can't have transparency to work with multiple pictureboxes on winforms. What you need to do is draw the images using Graphics like one over the other. Use 1 picturebox and do Graphics to overlay the picture on the picturebox. Or have the main picture as the form's background image and use pictureboxes, this was what I did. This is surely easier using WPF. Alternatively... Try to check this imaging SDK.
http://www.gdpicture.com/
This might help overlaying your images.
And this too, this is basically the one using 1 picturebox and overlaying using Graphics.
VB.NET Winforms: Overlay two transparent images

What is the use of System.Drawing.Graphics.Clear(Color pColor) method?

Well, I'm resizing an image in VB.NET. I have predefined algorithm that converts the image into particular size.
I just found a bug in the algorithm which causes white linings on right side of the image. After debugging the code i found that,
Dim grReturn As Drawing.Graphics = Drawing.Graphics.FromImage(bmpReturn)
grReturn.Clear(Color.White)
causes the issue. I tried googling the issue and found just a single MSDN link which says it clears the entire drawing surface and fills it with the specified background color.
And i am not able to understand what it says!
I am able to remove white linings from my output image by commenting this Clear function. As i don't know what this function actually does, i can not remove it.
So if anyone could help me understanding the method? Will it cause any other issue if i remove this method?
Thanks!
Clear method simply draws the background of the graphics to the provided color code.
As per Varocarbas comment above question,
"The explanation you are posting sounds pretty descriptive: it sets
the background of the graphic (future image) to the given color. From
what you are saying, your problem is not provoked because this code
"draws lines", but because it makes the whole background white and the
size of bmpReturn does not occupy all this background (and thus the
white lines you see are not more than parts of the background). If you
don't want this effect, just don't use this line of code; and, in
general, you shouldn't use code whose exact functionality is not clear
to you"

Translate Colors to Image?

Im not sure how else I should approach it, but if I was to (in my mac application) have a grid of NSViews, which the user can change the colour of each, is it possible to then translate this, so now I have been given a colour for each pixel by the user, make this into an exportable image?
I honestly can't think of how else to do this. I don't want to go ahead an realise I have taken a rather foolish path.
The idea is I will have a grid of squares which the user can paint, a colour in each square, a square representing a pixel in the final image. So they paint with like a paint bucket filling each one, then export it into an actual image file.
Any help much appreciated, thanks.
A grid of NSViews sounds really heavy for what you're doing. Why not write one single custom view that checks the mouse position and modifies the data appropriately? Then you'd write a custom drawing method to fill the custom view, and you could use the same exact draw method to write to an NSImage which you could export.
You'll need to do a bit o' math. For each "pixel", call -set on the appropriate NSColor, then use NSBezierPath's -fillRect method. It may help you to get out a pencil & paper to figure out the math for the rect origins & sizes.
Check http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaDrawingGuide/Introduction/Introduction.html for help if you've never done custom drawing before. It's really not that bad, just takes a little reading. :)

Manipulating / Resizing / Scaling an image in vb.net

Imagine I have a rectangle say 400px x 300px. Then let’s say I want to load an image in that. All of this is very easy using Sytem.Drawing.DrawImage.
Rectangle http://img576.imageshack.us/img576/2363/rectangle.gif
But then I want to leave the left hand side as 300px but change the right hand side to 250 px. I can draw the box using 4 DrawLines but I don’t know how to squash the image into the new shape. I want the right hand side of the shape to be 250, the left size 300 and the top and bottom 400px.
Resized http://img85.imageshack.us/img85/3479/rectangle2.gif
I can’t use DrawImage as it expects the left and right sizes to be the same. Is there a way to manipulate the image into the new shape?
I've looked at other questions, but they only apply where the left and right hand side is equal.
Any thoughts on how to squash an image into a shape which did not have parallel sides?
(If it helps, I'm happy to sacrifice image quality to fit the right shape.)
Disclaimer: I work for Atalasoft.
Our DotImage product has a command called QuadrilateralWarpCommand that can do this. It's in DotImage Photo.
What you want to do is non-trivial (but also very powerful).
#Heinzi is correct, the general class is called warp transformations. What you're trying to do is specifically a perspective transformation. At a high level, it involves running the individual coordinates through a transformation matrix to get their new positions, and then doing interpolation between pixel values based on the old and new locations.
This article talks about some transformations, one of them being a sheer, so it might be helpful overall. I'm not sure, I haven't read it closely. In general, you want to google for something approximately like "c# image transformation" or "c# image perspective transform".
Depending on what you're planning on using it for, buying a library might be the best way to go about it, although there is a lot to learn about image manipulation by doing it yourself.
I did not find a solution to your problem, but I have some information which might help you along:
What you want is called a warp transformation.
As far as I know, the .net framework natively supports this kind of transformation only for a GraphicsPath, namely, the GraphicsPath.Warp method. Unfortunately, I don't think that this will help you, unless you are willing to redraw your image using a .net GraphicsPath object.
If you need the transformation directly in the UI layer, your UI library might help: Silverlight, for example, includes the PlaneProjection class, which can be used for such effects; in WPF, the 3D engine might be useful for this (requiring more programming effort, through).