Crop and load tiled image in picturebox c# - c#

I have a tiled image like https://i.imgsafe.org/67397f9.png and want to load its parts as image for a picture box in some mouse events. actually I want simulate button behavior.
Bitmap source = new Bitmap(Properties.Resources.btn_close);
public Bitmap CropImage(Bitmap srcbmp, Rectangle dstcrp)
{
Bitmap bmp = new Bitmap(dstcrp.Width, dstcrp.Height);
Graphics gfx = Graphics.FromImage(bmp);
gfx.DrawImage(srcbmp, 0, 0, dstcrp, GraphicsUnit.Pixel);
return bmp;
}
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
Rectangle section = new Rectangle(new Point(0, 93), new Size(51, 30));
pictureBox1.Image = CropImage(source, section);
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
Rectangle section = new Rectangle(new Point(0, 93), new Size(51, 30));
pictureBox1.Image = CropImage(source, section);
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
Rectangle section = new Rectangle(new Point(0, 62), new Size(51, 30));
pictureBox1.Image = CropImage(source, section);
}
private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
Rectangle section = new Rectangle(new Point(0, 0), new Size(51, 30));
pictureBox1.Image = CropImage(source, section);
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
Rectangle section = new Rectangle(new Point(0, 0), new Size(51, 30));
pictureBox1.Image = CropImage(source, section);
}
this is my codes that crop sections of image and load as bitmap for picture box.
I think it is not very professional and may have some memory usage problems, etc...
is there any simple way to do this?

2 solutions which i can think of
When you first load the form you can declare 4 image/bitmap variables 1 for each Mouse State Mouse Down, Leave Up and Enter so instead of recrating the images again and again you can just change the image when time is right.
var cropCoordinates= new Rectangle(new Point(0, 0), new Size(51, 30));
var onMouseDownImage = new Bitmap(Properties.Resources.btn_close);
You can have 4 different image boxes for each state (layered 1 on top of the other )and show or hide when the time is needed

Related

Getting Printing issue in C# windows forms

I'm trying to print windows forms and it is printing well but the issue is I want to print the form to the whole page. As you guys can see the the end of the page is empty. Is there a way to stretch is or something as solution. I'm new to C# and windows forms and don't know much, kindly anyone help !!. Explanation which is easy to understand.
here is my code:
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawImage(bitmap, 0, 0);
}
private void Print_Click(object sender, EventArgs e)
{
Panel panel = new Panel();
this.Controls.Add(panel);
Graphics grp = panel.CreateGraphics();
Size formSize = this.ClientSize;
bitmap = new Bitmap(formSize.Width, formSize.Height, grp);
grp = Graphics.FromImage(bitmap);
Point panelLocation = PointToScreen(panel.Location);
grp.CopyFromScreen(panelLocation.X, panelLocation.Y, 0, 0, formSize);
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.PrintPreviewControl.Zoom = 1;
printPreviewDialog1.ShowDialog();
}
Bitmap bitmap;
private void CaptureScreen()
{
Graphics myGraphics = this.CreateGraphics();
Size s = this.Size;
bitmap = new Bitmap(s.Width, s.Height, myGraphics);
Graphics memoryGraphics = Graphics.FromImage(bitmap);
memoryGraphics.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, s);
}

I can not find the error in the code.a picture Box does not update

I'm creating a software in which pictureBox2 updates with the color of the pixel clicked on pictureBox1.
Already tried to use refresh(), but pictureBox2 does not change.
private void PictureBox1_MouseUp(object sender, MouseEventArgs e)
{
Bitmap b = new Bitmap(pictureBox1.Image);
color = b.GetPixel(e.X, e.Y); // Color
solidColor = new Bitmap(pictureBox2.Width, pictureBox2.Height, PixelFormat.Format24bppRgb); //Image
using (Graphics grp = Graphics.FromImage(solidColor))
{
SolidBrush co = new SolidBrush(color);
grp.FillRectangle( co, 0, 0, pictureBox2.Width, pictureBox2.Height);
}
pictureBox2.Image = solidColor;
}
I was able to solve this by doing this.
private void PictureBox1_Click(object sender, EventArgs e)
{
Bitmap b = new Bitmap(pictureBox1.Image);
MouseEventArgs me = (MouseEventArgs)e;
Point cord= me.Location;
color = b.GetPixel(cord.X,cord.Y);
solidColor = new Bitmap(pictureBox2.Width, pictureBox2.Height, PixelFormat.Format24bppRgb);
using (Graphics grp = Graphics.FromImage(solidColor))
{
SolidBrush co = new SolidBrush(color);
grp.FillRectangle(co, 0, 0, pictureBox2.Width, pictureBox2.Height);
}
pictureBox2.Image = solidColor;
}
```c#

Using the correct way to draw on a picturebox messes up the output

Im using a picturebox as a preview. When I was doing it incorrectly, the picturebox matched the printed output. When I changed it to use a bitmap, it looks poorly. I checked the DpiX and DpiY of the graphics object and it is 96 for all of them. The issue can be demonstrated with the following code. Create a form with a button and picturebox. Click the button. Then uncomment the line "//Correctway = true;" and observe the difference.
public partial class Form1 : Form
{
private bool Correctway;
public Form1()
{
InitializeComponent();
button1.Click += Button1_Click;
}
private void Button1_Click(Object sender, EventArgs e)
{
PrintPageEventArgs eOutput;
Graphics g;
string OutputText;
Font PrintFont;
Bitmap Output;
//Correctway = true;
OutputText = "CERTIFICATION";
PrintFont = new Font("Arial", 16, FontStyle.Regular);
Output = new Bitmap(850, 1100);
if (Correctway)
g = Graphics.FromImage(Output);
else
g = pictureBox1.CreateGraphics();
eOutput = new PrintPageEventArgs(g, new Rectangle(new Point(25, 25), new Size(new Point(825, 1075))), new Rectangle(new Point(0, 0), new Size(new Point(850, 1100))), new PageSettings());
eOutput.Graphics.DrawString(OutputText, PrintFont, Brushes.Black, 0, 0);
eOutput.Graphics.DrawRectangle(Pens.Gray, 20, 30, Output.Width - 100, Output.Height - 130);
if (Correctway)
pictureBox1.Image = Output;
}
}
"Poor" output. I wish I could describe it better, but I can't figure out what is happening.
This is the desired output, the output that is the result of printing and the screen output when I was using the incorrect procedure of creating a graphics object from the picturebox.
Note: I tried "You can also press CTRL+G to insert an image." but it does not work. With or without the image on the clipboard.
Taw found the answer in his comments. I am publishing it here to complete this post. The solution was to use g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
public partial class Form1 : Form
{
private bool Correctway;
public Form1()
{
InitializeComponent();
button1.Click += Button1_Click;
}
private void Button1_Click(Object sender, EventArgs e)
{
PrintPageEventArgs eOutput;
Graphics g;
string OutputText;
Font PrintFont;
Bitmap Output;
//Correctway = true;
OutputText = "CERTIFICATION";
PrintFont = new Font("Arial", 16, FontStyle.Regular);
Output = new Bitmap(850, 1100);
if (Correctway)
g = Graphics.FromImage(Output);
else
g = pictureBox1.CreateGraphics();
eOutput = new PrintPageEventArgs(g, new Rectangle(new Point(25, 25), new Size(new Point(825, 1075))), new Rectangle(new Point(0, 0), new Size(new Point(850, 1100))), new PageSettings());
eOutput.Graphics.DrawString(OutputText, PrintFont, Brushes.Black, 0, 0);
eOutput.Graphics.DrawRectangle(Pens.Gray, 20, 30, Output.Width - 100, Output.Height - 130);
if (Correctway)
pictureBox1.Image = Output;
}
}

How to save and load in panel?

I want to save image from panel to bitmap and then I want to load the saved image after my Form comes out of minimizing mode.
Bitmap bmp = new Bitmap(panel1.Width, panel1.Height);
panel1.DrawToBitmap(bmp, panel1.Bounds);
bmp.Save(#"C:\Test");
panel1.BackgroundImage = Image.FromFile(#"C:\Test");
And what event should I use for minimizing event?
P.S. I am a C# beginner.
EDITED
Drawing your panel's contents. This should be done inside its Paint event handler, like this:
private void panel1_Paint(object sender, PaintEventArgs e)
{
using (Pen p = new Pen(Color.Red, 3))
{
// get the panel's Graphics instance
Graphics gr = e.Graphics;
// draw to panel
gr.DrawLine(p, new Point(30, 30), new Point(80, 120));
gr.DrawEllipse(p, 30, 30, 80, 120);
}
}
Saving your panel's contents as an image. This part should be done somewhere else (for example, when you click on a "Save" button):
private void saveButton_Click(object sender, EventArgs e)
{
int width = panel1.Size.Width;
int height = panel1.Size.Height;
using (Bitmap bmp = new Bitmap(width, height))
{
panel1.DrawToBitmap(bmp, new Rectangle(0, 0, width, height));
bmp.Save(#"C:\testBitmap.jpeg", ImageFormat.Jpeg);
}
}

select, cut and replace a region of image with another [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have a picture box and import image to this picture box. I want select a region of this picture box (image) like "free-form selection in MS Paint" then cut selected region and replace (fill) this region with another image or a system color.
What is your idea for this problem?!
Thanks.
I wrote a little winform application. There are a lots of things to improve here, but it can give you some advice on how to start.
I used three pictureboxes, one where I displayed the image without changes, a transparent picturebox to select the part of image that I'd like to cut and another one where I can paste the part of the image.
MAIN CODE:
Select:
//I used a rectangle to "select" the part of image
Rectangle imageRegion = new Rectangle(clickedPointOne, pbImageRegion.Size); //clickedPointOne is the point of image where I start to select and pbImageRegion.Size is the size of the part of image.
//Then I cloned the part of image that I want
Image newImage = image.Clone(imageRegion, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
Cut:
//I created a new bitmap with the size of the part of image that I selected
Bitmap bmp = new Bitmap(newImage.Width, newImage.Height);
Graphics g = Graphics.FromImage(bmp);
//So here I drawed in the bitmap a rectangle with the picturebox backcolor that rappresent the "blank" part of image
g.FillRectangle(new SolidBrush(mainPictureBox.BackColor), new Rectangle(new Point(0, 0), newImage.Size));
//Now draw the "blank" part on the main image
g = Graphics.FromImage(image);
g.DrawImage(bmp, clickedPointOne);
Replace: (In my application you can paste the part of image wherever in the secondPictureBox)
//Get graphics from the picturebox image (where there could be another image)
Graphics g = Graphics.FromImage(secondPictureBox.Image);
//Draw the part of image
g.DrawImage(newImage, clickedPointTwo); //newImage is the part of image selected and cut, clickedPointTwo is the point of upper-left corner where you want to begin draw the image
THE WHOLE CODE:
private Bitmap image;
private Bitmap newImage;
private Rectangle imageRegion;
private PictureBox pbImageRegion;
private Point clickedPointOne;
private Point clickedPointTwo;
private bool allowMouseMove;
private bool clickedCutButton;
private bool firstClick;
public Form1()
{
InitializeComponent();
mainPictureBox.BackColor = Color.White;
secondPictureBox.BackColor = Color.White;
}
private void loadImageButton_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
image = new Bitmap(ofd.FileName);
mainPictureBox.Image = image;
Bitmap bmp = new Bitmap(image.Width, image.Height);
Graphics g = Graphics.FromImage(bmp);
g.FillRectangle(new SolidBrush(Color.White), new Rectangle(new Point(0, 0), secondPictureBox.Size));
secondPictureBox.Image = bmp;
}
}
private void cutImageButton_Click(object sender, EventArgs e)
{
firstClick = false;
clickedCutButton = true;
allowMouseMove = false;
pbImageRegion = new PictureBox();
pbImageRegion.BackColor = Color.Transparent;
pbImageRegion.BorderStyle = BorderStyle.FixedSingle;
pbImageRegion.Size = new Size(0, 0);
pbImageRegion.MouseMove += new MouseEventHandler(pbImageRegion_MouseMove);
}
void pbImageRegion_MouseMove(object sender, MouseEventArgs e)
{
if (allowMouseMove == true)
pbImageRegion.Size = new Size(Math.Abs(e.X - clickedPointOne.X - 2), Math.Abs(e.Y - clickedPointOne.Y - 2));
}
private void mainPictureBox_MouseClick(object sender, MouseEventArgs e)
{
if (clickedCutButton == true)
{
if (e.Button == MouseButtons.Left)
{
if (firstClick == false)
{
pbImageRegion.Location = new Point(e.X, e.Y);
mainPictureBox.Controls.Add(pbImageRegion);
clickedPointOne = new Point(e.X, e.Y);
allowMouseMove = true;
firstClick = true;
}
else
{
imageRegion = new Rectangle(clickedPointOne, pbImageRegion.Size);
newImage = image.Clone(imageRegion, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
allowMouseMove = false;
mainPictureBox.Invalidate();
}
}
}
}
private void mainPictureBox_MouseMove(object sender, MouseEventArgs e)
{
//It works only from left to right
if (allowMouseMove == true)
pbImageRegion.Size = new Size(Math.Abs(e.X - clickedPointOne.X - 2), Math.Abs(e.Y - clickedPointOne.Y - 2));
}
private void secondPictureBox_MouseClick(object sender, MouseEventArgs e)
{
if (clickedCutButton == true)
{
if (e.Button == MouseButtons.Left)
{
clickedCutButton = false;
pbImageRegion.Size = new Size(0, 0);
clickedPointTwo = new Point(e.X, e.Y);
secondPictureBox.Invalidate();
}
}
}
private void secondPictureBox_Paint(object sender, PaintEventArgs e)
{
if (newImage != null)
{
Graphics g = Graphics.FromImage(secondPictureBox.Image);
g.DrawImage(newImage, clickedPointTwo);
e.Graphics.DrawImage(secondPictureBox.Image, new Point(0, 0));
}
}
private void mainPictureBox_Paint(object sender, PaintEventArgs e)
{
if (newImage != null && allowMouseMove == false)
{
Bitmap bmp = new Bitmap(newImage.Width, newImage.Height);
Graphics g = Graphics.FromImage(bmp);
g.FillRectangle(new SolidBrush(mainPictureBox.BackColor), new Rectangle(new Point(0, 0), newImage.Size));
g = Graphics.FromImage(image);
g.DrawImage(bmp, clickedPointOne);
}
}

Categories