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);
}
Related
private void pictureBox2_Paint(object sender, PaintEventArgs e)
{
Bitmap bmp = new Bitmap(pictureBox1.Image);
// Lock the bitmap's bits.
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
System.Drawing.Imaging.BitmapData bmpData =
bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
bmp.PixelFormat);
// Get the address of the first line.
IntPtr ptr = bmpData.Scan0;
// Declare an array to hold the bytes of the bitmap.
int bytes = Math.Abs(bmpData.Stride) * bmp.Height;
byte[] rgbValues = new byte[bytes];
// Copy the RGB values into the array.
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
// Set every third value to 255. A 24bpp bitmap will look red.
//for (int counter = 2; counter < rgbValues.Length; counter +=64)
// rgbValues[counter] = 255;
// Copy the RGB values back to the bitmap
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);
// Unlock the bits.
bmp.UnlockBits(bmpData);
// Draw the modified image.
e.Graphics.DrawImage(bmp, 0, 0);
}
i mean to see in pictureBox2 the image get fill slowly like a paint get painted each time a bit. and not at once. with the original colors of the image in pictureBox1 to copy the image in pictureBox1 to pictureBox2 buti nstead in once to make it slowly and each time copy some pixels or one by one until the whole image paint is completed in pictureBox2.
I tried this.
in time tick event :
int cc = 0;
private void timer1_Tick(object sender, EventArgs e)
{
cc++;
pictureBox2.Invalidate();
}
in pictureBox2 paint event
private void pictureBox2_Paint(object sender, PaintEventArgs e)
{
Bitmap bmp = new Bitmap(pictureBox1.Image);
// Lock the bitmap's bits.
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
System.Drawing.Imaging.BitmapData bmpData =
bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
bmp.PixelFormat);
// Get the address of the first line.
IntPtr ptr = bmpData.Scan0;
// Declare an array to hold the bytes of the bitmap.
int bytes = Math.Abs(bmpData.Stride) * cc;//bmp.Height;
byte[] rgbValues = new byte[bytes];
// Copy the RGB values back to the bitmap
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);
// Unlock the bits.
bmp.UnlockBits(bmpData);
// Draw the modified image.
e.Graphics.DrawImage(bmp, 0, 0);
}
but the code in the pictureBox2 paint event delete the image in the pictureBox2 delete slowly from the top to the bottom.
but i want the opposite that it will start that the pictureBox2 is clear and then the image will be painted slowly.
i tried to change the line :
Bitmap bmp = new Bitmap(pictureBox1.Image);
to
Bitmap bmp = new Bitmap(512, 512);
but then it does nothing in the pictureBox2.
Here's an example that will copy the image line by line:
private async void button1_Click(object sender, EventArgs e)
{
if (pictureBox1.Image != null)
{
button1.Enabled = false;
Bitmap bmp1 = new Bitmap(pictureBox1.Image);
Bitmap bmp2 = new Bitmap(bmp1.Width, bmp1.Height);
pictureBox2.Image = bmp2;
using (Graphics G = Graphics.FromImage(bmp2))
{
for (int y = 0; y < bmp1.Height; y++)
{
Rectangle rc = new Rectangle(new Point(0, y), new Size(bmp1.Width, 1));
G.DrawImage(bmp1, rc, rc, GraphicsUnit.Pixel);
pictureBox2.Invalidate();
await Task.Delay(1);
}
}
button1.Enabled = true;
}
}
Sample run:
To make it go faster, you can increase the height of the rectangle being copied, and then make the for loop jump by that much:
int height = 12; // added
for (int y = 0; y < bmp1.Height; y = y + height) // change
{
Rectangle rc = new Rectangle(new Point(0, y), new Size(bmp1.Width, height)); // change
G.DrawImage(bmp1, rc, rc, GraphicsUnit.Pixel);
pictureBox2.Invalidate();
await Task.Delay(1);
}
Here's another approach showing a radial expansion using a GraphicsPath, Region, and a Clip:
private async void button2_Click(object sender, EventArgs e)
{
if (pictureBox1.Image != null)
{
button2.Enabled = false;
Bitmap bmp1 = new Bitmap(pictureBox1.Image);
Bitmap bmp2 = new Bitmap(bmp1.Width, bmp1.Height);
pictureBox2.Image = bmp2;
int radius = Math.Max(bmp1.Width, bmp1.Height);
Point center = new Point(bmp1.Width / 2, bmp2.Height / 2);
using (Graphics G = Graphics.FromImage(bmp2))
{
int step = 10;
for (int r=0; r <=radius; r=r+step)
{
Rectangle rc = new Rectangle(center, new Size(1, 1));
rc.Inflate(r, r);
using (System.Drawing.Drawing2D.GraphicsPath gp = new System.Drawing.Drawing2D.GraphicsPath())
{
gp.AddEllipse(rc);
using (Region rgn = new Region(gp))
{
G.Clip = rgn;
G.DrawImage(bmp1, 0, 0);
}
}
pictureBox2.Invalidate();
await Task.Delay(1);
}
}
button2.Enabled = true;
}
}
Play with the step value to make it go faster or slower:
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 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;
}
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);
}