I am try Try capture an image of an particular area in my webpage. Below code works fine but issue is it gives me only a blank image with a size of 1.64kb every time.
Function call
protected void btn_Click(object sender, EventArgs e)
{
System.Drawing.Image image = CaptureScreen(50, 99, 930, 450);
image.Save(Server.MapPath("/img/screen.jpg"));
}
Function Definition
public System.Drawing.Image CaptureScreen(int sourceX, int sourceY, int destX, int destY)
{
Bitmap bmp = new Bitmap(930, 430);
Graphics g = Graphics.FromImage(bmp);
Size xx=new Size();
xx.Width=930;
xx.Height=450;
g.CopyFromScreen(sourceX, sourceY, destX, destY, xx);
return bmp;
}
Related
How it should be
'how to show checkbox on saved bitmap?'
reality
private void picWyslij_Click(object sender, EventArgs e)
{int width = panel12.Size.Width; int height = panel12.Size.Height;
Bitmap bm = new Bitmap(width, height);
panel12.DrawToBitmap(bm, new Rectangle(0, 0, width, height));
bm.Save(#"C:\Users\zooma\Desktop\IMAGE\TestDrawToBitmap.bmp", ImageFormat.Bmp);
}
I need to save picturebox loaded png file with picturebox backgroud colour.
I've try with these.
But result is showing as a blank image.
help me..
private void bunifuThinButton24_Click(object sender, EventArgs e)
{
Bitmap bmp1 = new Bitmap(pictureBox1.Width, pictureBox1.Height, pictureBox1.CreateGraphics());
bmp1.Save(#"C:\Users\...\New folder4\ xx.jpg");
}
Bitmap bmp1 = new Bitmap(pictureBox1.Width, pictureBox1.Height, pictureBox1.CreateGraphics()); creates a new blank bitmap image with the resolution of pictureBox1's graphics object. you need to draw your picturebox in this bitmap. You can do this using the picturebox's DrawToBitmap function.
void bunifuThinButton24_Click(object sender, EventArgs e)
{
Bitmap bmp1 = new Bitmap(pictureBox1.Width, pictureBox1.Height, pictureBox1.CreateGraphics());
// fill in bitmap with the picturebox's backcolor
using (Graphics g = Graphics.FromImage(bmp1))
{
using (Brush drawBrush = new SolidBrush(pictureBox1.BackColor))
{
g.FillRectangle(drawBrush, new Rectangle(0, 0, bmp1.Width, bmp1.Height));
}
}
// draw picturebox on bitmap
pictureBox1.DrawToBitmap(bmp1, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height));
bmp1.Save(#"C:\Users\...\New folder4\ xx.jpg");
}
I'm trying to achieve an effect similar to this site. I am basically trying to implement a way to "compare" two similar images (with different colors, etc). I managed to do this in a not so brilliant way using two PictureBox controls (Winforms) one next to the other, and changing their Size and Location attributes on a MouseMove event.
The result works, but it flickers a lot and it's not really the best way to do it.
Is there a better way to do this, maybe with a WPF or by changing the code in any way? Here it is:
private void pbImg1_MouseMove(object sender, MouseEventArgs e)
{
pbImg2.Image = CropImage(array[1], new Rectangle(pbImg1.Size.Width, 0, totalSize.Width - pbImg1.Size.Width, 240));
pbImg1.Size = new Size(e.X, pbImg1.Height);
pbImg2.Location = new Point(pbImg1.Size.Width + pbImg1.Location.X, pbImg2.Location.Y);
pbImg2.Size = new Size(totalSize.Width - pbImg1.Size.Width, 240);
lpbImg1Size.Text = pbImg1.Size.ToString();
lpbImg2Size.Text = pbImg2.Size.ToString();
lpbImg1Location.Text = pbImg1.Location.ToString();
lpbImg2Location.Text = pbImg2.Location.ToString();
}
private void pbImg2_MouseMove(object sender, MouseEventArgs e)
{
pbImg1.Image = CropImage(array[0], new Rectangle(0, 0, totalSize.Width - pbImg2.Size.Width, 240));
pbImg1.Size = new Size(pbImg1.Width + e.X, 240);
lpbImg1Size.Text = pbImg1.Size.ToString();
lpbImg2Size.Text = pbImg2.Size.ToString();
lpbImg1Location.Text = pbImg1.Location.ToString();
lpbImg2Location.Text = pbImg2.Location.ToString();
}
public Bitmap CropImage(Bitmap source, Rectangle section)
{
// An empty bitmap which will hold the cropped image
//TRY CATCH
Bitmap bmp = new Bitmap(section.Width, section.Height);
Graphics g = Graphics.FromImage(bmp);
// Draw the given area (section) of the source image
// at location 0,0 on the empty bitmap (bmp)
g.DrawImage(source, 0, 0, section, GraphicsUnit.Pixel);
return bmp;
}
And here you can see the behavior of the program:
https://gfycat.com/VillainousReadyAmazonparrot
You need two images imageLeft, imageRight with the same size as the picturebox:
private int pos = 0; //x coordinate of mouse in picturebox
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if(pos == 0)
{
e.Graphics.DrawImage(Properties.Resources.imageRight, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height));
}
else
{
e.Graphics.DrawImage(Properties.Resources.imageLeft, new Rectangle(0, 0, pos, pictureBox1.Height),
new Rectangle(0, 0, pos, pictureBox1.Height), GraphicsUnit.Pixel);
e.Graphics.DrawImage(Properties.Resources.imageRight, new Rectangle(pos, 0, pictureBox1.Width - pos, pictureBox1.Height),
new Rectangle(pos, 0, pictureBox1.Width - pos, pictureBox1.Height), GraphicsUnit.Pixel);
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
pos = e.X;
pictureBox1.Invalidate();
}
I have Image of width 1171 and height 1181. I need that image into print page using printDocument...So Final image size would be width 596 and height 892. So it covers all area of page without any space.
My problem is I am able to resize it but it does not cover the whole space of page...And with different solution it covers the whole page area but image cutted down.
So what I have to do for solving this issue?
My code as per below...
private void btnPrint_Click(object sender, EventArgs e)
{
btnPrint.Enabled = false;
try
{
printDocument1.Print();
this.Close();
}
catch (Exception err)
{
MessageBox.Show("Printing failed :" + err.Message);
btnPrint.Enabled = true;
}
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Image img = ((PictureBox)pictureBox1).Image;
Bitmap bmp = (Bitmap)img;
bmp.Save("abcc");
Bitmap bitmap = new Bitmap(bmp, new Size(892, 596));
// Bitmap b = ResizeBitmap(bmp, 892, 596);
Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
e.Graphics.CompositingMode = CompositingMode.SourceCopy;
e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
e.Graphics.Transform.Scale(0, 0);
e.Graphics.DrawImage(bitmap,new Rectangle(0,0,892,596),new Rectangle(0,0,bmp.Width,bmp.Height),GraphicsUnit.Pixel);
}
I have made an application, and i need the function drawbitmap to print my panel. When i push the button (btnUpdate) 12 times or more I get a parameter exception(invalid parameter)on this rule: panel1.DrawToBitmap(bmp1, new Rectangle(0, 0, 2480, 3508));
private void preview()
{
Bitmap bmp1 = new Bitmap(2480, 3508);
panel1.DrawToBitmap(bmp1, new Rectangle(0, 0, 2480, 3508));
pictureBox2.Image = bmp1;
}
private void btnUpdate_Click(object sender, EventArgs e)
{
preview();
}
Can someone help me please?
I can't use the bmp1.Dispose(); function... I get an exeption in the Program.cs file in this line: Application.Run(new Form1());
This could be a case of not disposing the bitmaps when you're done with them. Try this:
panel1.DrawToBitmap(...);
// get old image
Bitmap oldBitmap = pictureBox2.Image as Bitmap;
// set the new image
pictureBox2.Image = bmp1;
// now dispose the old image
if (oldBitmap != null)
{
oldBitmap.Dispose();
}
You have a great big Memory leak there, watch your memory as you click the button 12 clicks and your up to 1GB,
try declaring you Bitmap as a varable and Dispose it before re assigning.
private Bitmap bmp1;
private void preview()
{
if (bmp1 != null)
{
bmp1.Dispose();
}
bmp1 = new Bitmap(2480, 3508);
panel1.DrawToBitmap(bmp1, new Rectangle(0, 0, 2480, 3508));
pictureBox2.Image = bmp1;
}
Or just clear the PictureBox befor assigning a new Bitmap
private void preview()
{
if (pictureBox2.Image != null)
{
pictureBox2.Image.Dispose();
}
Bitmap bmp1 = new Bitmap(2480, 3508);
panel1.DrawToBitmap(bmp1, new Rectangle(0, 0, 2480, 3508));
pictureBox2.Image = bmp1;
}
The problem is fixed by doing this:
private void preview()
{
Bitmap bmp1 = new Bitmap(2480, 3508);
panel1.DrawToBitmap(bmp1, new Rectangle(0, 0, 2480, 3508));
Image img = pictureBox2.Image;
pictureBox2.Image = bmp1;
if (img != null) img.Dispose(); // the first time it'll be null
}
private void btnUpdate_Click(object sender, EventArgs e)
{
preview();
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
}