I need to render an image on top of a drawn Object - c#

I need to make a program that'll enable the user to customize his own car.
My problem is I have to draw the customizables by code and I have to add a PNG image of details on top of the drawn car.
The user has to select the colors, rim designs, and decals from the right
The car will be drawn when the PIMP button is pressed.
I have to add the PNG image, the second image, on top of the drawn image(first image), to make it look like the third image.
My current code looks like:
private void button1_Click(object sender, EventArgs e)
{
Graphics g;
g = this.CreateGraphics();
if (color == 1)
{
g.FillPolygon(blue, body);
}
else if (color ==2)
{
g.FIllPolygon(red, body);
}
g.FillPolygon(blackBrush, window);
pCard.Visible = True;
//pCard is an existing PictureBox where the Image is the cardetails.PNG
backcolor = transparent
}
When I press the PIMP button it draws the first image, but when it draws the PictureBox of cardetails.png, the transparent color displays the color gray and covers the first image.
I am very new to C# and Visual Basic. The only thing i know how to do here is to draw that blue car.

Load the image that contains the details you want to add:
Image decalImage = Image.FromFile("cardetails.png");
It would probably be best if you do not load it in your button1_Click method.
Then draw the image upon your graphics object using
g.DrawImage(decalImage, x, y);
Where x and y would be the position to draw it to.

Related

Saving off picturebox composed of image and drawings

Here is what I am trying to do. I have an Image that is 1920X1080. I am showing that image in a PictureBox and allowing a user to draw an ellipse on the screen. Once they finish that I will need to save that image off with the image and ellipse in 1 photo.
So I have tried several ways:
1. Just trying to save the image and ellipse from the PictureBox. No success doing that.
2. To store the location of the ellipse on the picture box and then redraw that ellipse on a new copy of the image using a graphics object. The problem with this one is that when it saves off the ellipse is not in the right place due to the size of the PictureBox and the original image difference.
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
pictureBox1.Cursor = Cursors.Default;
if (isMoving)
{
Circles.Add(mouseDownPosition, mouseMovePosition);
}
isMoving = false;
}
Bitmap newImage = new Bitmap(new Bitmap(#"C:\Personal\test\Sample.jpg"));
Graphics g = Graphics.FromImage(newImage);
foreach (var circle in Circles)
{
g.DrawEllipse(new Pen(Color.Red, 3), new Rectangle(circle.Key, new Size(circle.Value.X - circle.Key.X, circle.Value.Y - circle.Key.Y)));
}
newImage.Save(#"C:\Projects\Projects\SampleCombine.jpg");
I am really just looking for a way to take exactly what I see on the PictureBox and save it as its own jpg.
My take is that I need to figure out how to reposition the "Circle" based on where it was drawn and where it should be drawn on a larger file.
Any ideas?

how to crop a part of an image in c# and leave the white part, where it has been cut?

I have a problem. I want to cut a part of an image, but not to save the part that was cut. I want to have the image I had before with white shape, where the part which I cut was before.
Can you help?
I want to cut a part of an image, but not to save the part that was cut.
Surely you just want to draw on the image then?
If that's the case use the Graphics class and draw a filled in Rectangle on top of it.
public void RemoveSectionFromImage(Bitmap bitmap, Rectangle section, Color color)
{
using (Graphics g = Graphics.FromImage(bitmap))
using (SolidBrush brush = new SolidBrush(color))
{
g.FillRectangle(brush, section);
}
}

PictureBox - Handle Click Event on Non-Transparent Area of Image

I have to make a windows form in C# where two PictureBox are overlapping. TopPictureBox is containing a transparent png picture. By default TopPictureBox can be clicked by clicking any visible or transparent area of the image in TopPictureBox. But I want to make that TopPictureBox only can be clicked by clicking visible area of image, not in transparent area. Also I want to make that cursor will only change on the visible area of the image, not in transparent area.
IS THERE ANY WAY TO DO THESE?
I am using this code to make TopPictureBox transparent.
TopPictureBox.BackColor = Color.Transparent;
Thank You for Help.
Checking if a position in PictureBox is Transparent or not depends on the Image and SizeMode property of PictureBox.
You can not simply use GetPixel of Bitmap because the image location and size is different based on SizeMode. You should first detect the size and location of Image based on SizeMode:
public bool HitTest(PictureBox control, int x, int y)
{
var result = false;
if (control.Image == null)
return result;
var method = typeof(PictureBox).GetMethod("ImageRectangleFromSizeMode",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
var r = (Rectangle)method.Invoke(control, new object[] { control.SizeMode });
using (var bm = new Bitmap(r.Width, r.Height))
{
using (var g = Graphics.FromImage(bm))
g.DrawImage(control.Image, 0, 0, r.Width, r.Height);
if (r.Contains(x, y) && bm.GetPixel(x - r.X, y - r.Y).A != 0)
result = true;
}
return result;
}
Then you can simply use HitTest method to check if the mouse is over a non-transparent area of PictureBox:
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (HitTest(pictureBox1,e.X, e.Y))
pictureBox1.Cursor = Cursors.Hand;
else
pictureBox1.Cursor = Cursors.Default;
}
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
if (HitTest(pictureBox1, e.X, e.Y))
MessageBox.Show("Clicked on Image");
}
Also setting BackColor to Color.Transparent only makes the PictureBox transparent relative to it's parent. For example if you have 2 PictureBox in a Form setting the transparent back color, just cause you see the background of form. To make a PictureBox which supports transparent background, you should draw what is behind the control yourself. You can find a TransparentPictureBox in this post: How to make two transparent layer with c#?
One way is to check whether the colour of the pixel where the user clicked, is the same as the background colour of the form. If yes, then the user clicked on a transparent area.
(Note : As Reza mentioned, this code can be used only when there are no overlapping PictureBoxes, i.e. only when the transparent area of the image is of the same colour as the Form's background)
Color pixelColour;
private void myPicturebox_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
pixelColour = ((Bitmap)myPicturebox.Image).GetPixel(point.X, point.Y);
if (this.BackColor == pixelColour)
{
// User clicked on transparent area
}
else
{
// User clicked on image
}
}
}

Draw rectangle in picture box C#

I have an application that shows different images in a pictureBox. There are a few other picture boxes that do other things and I want to keep the default paint method.
When the image is correct I have to draw a green rectangle and when the image is incorrect I don't have to do anything.
I have seen that it is needed to override the paint method to draw rectangles but if I override the method it will always draw the green rectangle.
EDIT:
I have written this method:
private void pictureBox_Paint(object sender, PaintEventArgs e)
{
Rectangle rect = new Rectangle(e.ClipRectangle.X, e.ClipRectangle.Y, e.ClipRectangle.Width, e.ClipRectangle.Height);
e.Graphics.DrawRectangle(new Pen(Color.Green, 20), rect);
}
And I call add the event to it like this:
pbFrontView.Paint += pictureBox_Paint;
pbLeftView.Paint += pictureBox_Paint;
So, I have many picture boxes and different images (this is in my main function)
if(drawRectanglePbFront)
//Draw rectangle in pbFrontView
if(drawRectanglePbLeft)
//Draw rectangle in pbLeft
...
Is there any way to know which pinture box has invoked "pictureBox_Paint" ? If, yes I could move "if(drawRectanglePbFront) ..." inside of "pictureBox_Paint" and manage everything there

C# giving a PictureBox the shape of a Image [duplicate]

This question already has answers here:
Irregular shaped Windows Form (C#)
(2 answers)
Closed 8 years ago.
I want to give a pictureBox the shape of an image I have. The image off course is rectangular, but there is the backgroundcolor that should be ignored, which leads to the desired form. So if there is a picture of a banana on a white background, I should have a pictureBox in shape of a banana at the end.. This is needed so that I can move shaped PictureBox in front of a AxWMPLib Mediaplayer without having the rectangle surrounding the banana covering all of the video.
My approach is to create a new class that extends PictureBox and then setting the region to the shape. Problem is, I don't know how to get the shape.
class ShapedPBox : PictureBox
{
public ShapedPBox()
{
this.Paint += this.shapedPaint;
}
void shapedPaint(object sender, paintEventArgs e)
{
System.Drawing.Drawing2D.GraphicsPath gP = new System.Drawing.Drawing2D.GraphicsPath();
//do something to give gP.addPicture? perhaps something with transparency key?
this.Region = new Region(graphicsPath);
}
public Image image;
}
You can set the SizeMode property of PictureBox to StretchImage and that would remove default background colour. Of course, the image will shrink or grow based on image and control size.
If you cannot resize the image and you know where the background is, following code could be used. All it does is that it gets colour from edge of image and sets that as background color of picture box.
Image image = Image.FromFile(#"c:\Image.png");
Bitmap bitmap = new Bitmap(image);
pictureBox1.BackColor = bitmap.GetPixel(0, 1);
pictureBox1.Image = image;

Categories

Resources