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");
}
Related
I'm trying to draw a rectangle over an image, the rectangle is drawn but the image disappears.
My code:
private void button12_Click(object sender, EventArgs e)
{
currentBitmap = (Bitmap)pictureBox1.Image;
Bitmap temp1 = new Bitmap(currentBitmap.Height, currentBitmap.Width);
Bitmap bmap = (Bitmap)temp1.Clone();
Graphics gr = Graphics.FromImage(bmap);
Pen pen = new Pen(Color.Red);
gr.DrawRectangle(pen, 120, 120, 100, 100);
currentBitmap = (Bitmap)bmap.Clone();
pictureBox1.Image = currentBitmap;
}
Put your drawing methods inside the pictureBox1 onPaint Method. Then you call that using
pictureBox1.Paint += picturebox1_OnPaint;
I fixed this.
It was an error on my code which I didnt realize, i was just taking the width and height of the picturebox instead of the image data.
I changed
Bitmap temp1 = new Bitmap(currentBitmap.Height, currentBitmap.Width);
To
Bitmap temp1 = new Bitmap(pictureBox1.Image);
This is likely due to the fact that your image is set in pictureBox1. Try making a new pictureBox to overlay the old one:
pictureBox1 = image itself
pictureBox2 = bitmap
Good luck!
In c# and Visual Studio Windows forms I have loaded an image into a picture box (pictureBox2) and then cropped it and show in another picture box (pictureBox3).
Now I want to save what is inside pictureBox3 as an image file.
How can I do this?
private void crop_bttn_Click(object sender, EventArgs e)
{
Image crop = GetCopyImage("grayScale.jpg");
pictureBox2.Image = crop;
Bitmap sourceBitmap = new Bitmap(pictureBox2.Image,
pictureBox2.Width, pictureBox2.Height);
Graphics g = pictureBox3.CreateGraphics();
g.DrawImage(sourceBitmap, new Rectangle(0, 0,
pictureBox3.Width, pictureBox3.Height), rectCropArea, GraphicsUnit.Pixel);
sourceBitmap.Dispose();
}
Never use control.CreateGraphics! Either draw into a Bitmap bmp using a Graphics g = Graphics.FromImage(bmp) or in the Paint event of a control, using the e.Graphics parameter..
Here is a cropping code that draws into a new Bitmap and that makes use of your controls etc but changes a few things:
It uses a Graphics object that is created from a new Bitmap
It make use of using clauses to make sure it won't leak
It takes the size of the pictureBox3.ClientSize so it won't include any borders..
private void crop_bttn_Click(object sender, EventArgs e)
{
Image crop = GetCopyImage("grayScale.jpg");
pictureBox2.Image = crop;
Bitmap targetBitmap = new Bitmap(pictureBox3.ClientSize.Width,
pictureBox3.ClientSize.Height);
using (Bitmap sourceBitmap = new Bitmap(pictureBox2.Image,
pictureBox2.ClientSize.Width, pictureBox2.ClientSize.Height))
{
using (Graphics g = Graphics.FromImage(targetBitmap))
{
g.DrawImage(sourceBitmap, new Rectangle(0, 0,
pictureBox3.ClientSize.Width, pictureBox3.ClientSize.Height),
rectCropArea, GraphicsUnit.Pixel);
}
}
if (pictureBox3.Image != null) pictureBox3.Image.Dispose();
pictureBox3.Image = targetBitmap;
targetBitmap.Save(somename, someFormat);
}
The alternative would be to..:
move all your code to the Paint event
replace the Graphics g = pictureBox3.CreateGraphics(); be Graphics g = e.Graphics;
insert these two lines to the click event:
Bitmap targetBitmap = new Bitmap(pictureBox3.ClientSize.Width,
pictureBox3.ClientSize.Height);
pictureBox3.DrawToBitmap(targetBitmap, pictureBox3.ClientRectangle);
The method PictureBox.CreateGraphics() should not be used unless you know what you are doing because it can cause some not-so-obvious problems. For example, in you scenario, the image in pictureBox3 will disappear when you minimize or resize the window.
A better way is to draw to a bitmap, which you also can save:
var croppedImage = new Bitmap(pictureBox3.Width, pictureBox3.Height);
var g = Graphics.FromImage(croppedImage);
g.DrawImage(crop, new Point(0, 0), rectCropArea, GraphicsUnit.Pixel);
g.Dispose();
//Now you can save the bitmap
croppedImage.Save(...);
pictureBox3.Image = croppedImage;
Btw, please use more reasonable variable names, especially for pictureBox1..3.
In form1 constructor
public Form1()
{
InitializeComponent();
BackColor = Color.LightGreen;
TransparencyKey = Color.LightGreen;
this.TopMost = true;
this.Location = new Point(0, 0);
timer1.Enabled = true;
}
Then:
private void Cap()
{
countImages++;
ScreenCapture sc = new ScreenCapture();
Image img = sc.CaptureScreen();
Bitmap bmp = new Bitmap(img);
Bitmap source = new Bitmap(this.Width, this.Height);
Rectangle section = new Rectangle(new Point(source.Width, source.Height), source.Size);
Bitmap CroppedImage = CropImage(source, section);
CroppedImage.Save("c:\\temp\\screens\\" + countImages + ".gif", ImageFormat.Gif);
source.Dispose();
}
And
public Bitmap CropImage(Bitmap source, Rectangle section)
{
// An empty bitmap which will hold the cropped image
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;
}
The Bitmaps i save to the hard disk:
CroppedImage.Save("c:\\temp\\screens\\" + countImages + ".gif", ImageFormat.Gif);
They are all black.
I want to crop this part from the image and save it to the hard disk.
Example of screenshot and what i want to crop from it:
I marked with a red circle the transparent form1 and this is what i want to crop the part of the form1 and save this as cropped image the form1 part:
I tried this first:
private void CaptureDesktop()
{
countImages++;
ScreenCapture sc = new ScreenCapture();
Image img = sc.CaptureScreen();
Bitmap bmp = new Bitmap(img);
Bitmap source = new Bitmap(this.Width, this.Height);
Rectangle section = new Rectangle(new Point(source.Width, source.Height), source.Size);
Bitmap CroppedImage = CropImage(bmp, section);
CroppedImage.Save("c:\\temp\\desktopscreens\\" + countImages + ".gif", ImageFormat.Gif);
}
But i'm getting this cropped image as result: Strange picture in picture ?
Then i tried this code:
using (Bitmap bmpScreenCapture = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height))
{
using (Graphics g = Graphics.FromImage(bmpScreenCapture))
{
g.CopyFromScreen(this.Location.X,
this.Location.Y,
0, 0,
this.Size,
CopyPixelOperation.SourceCopy);
}
Rectangle section = new Rectangle(new Point(this.Location.X, this.Location.Y), new Size(this.Width, this.Height));
Bitmap CroppedImage = CropImage(bmpScreenCapture, section);
CroppedImage.Save("c:\\temp\\desktopscreens\\1.bmp", ImageFormat.Bmp);
}
But the result is:
Not what i wanted. I want only to crop the form1 part from the whole image.
You do Bitmap source = new Bitmap(this.Width, this.Height); this creates a black image.
Then you proceed to crop it. That's still a black image.
Then you proceed to save it. That's still a black image.
Perhaps you meant to crop Bitmap bmp or Image img, not Bitmap source. I don't know what Bitmap source is meant to be.
Try this (some code from Capture the screen shot using .NET)
private void Cap()
{
using (Bitmap bmpScreenCapture = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height))
{
using (Graphics g = Graphics.FromImage(bmpScreenCapture))
{
g.CopyFromScreen(this.Location.X,
this.Location.Y,
0, 0,
this.Size,
CopyPixelOperation.SourceCopy);
}
Rectangle section = new Rectangle(new Point(this.Location.X, this.Location.Y), new Size(this.Width, this.Height));
Bitmap CroppedImage = CropImage(bmpScreenCapture, section);
CroppedImage.Save("c:\\temp\\screens\1.bmp", ImageFormat.Bmp);
}
}
public Bitmap CropImage(Bitmap source, Rectangle section)
{
// An empty bitmap which will hold the cropped image
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;
}
I try to do double buffer using BufferedGraphics. When i use BufferedGraphics.Render method background of my image changes to black. Here the simple code, that illustrate my issue
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
this.Load += Form1_Load;
}
private void Form1_Load(object sender, EventArgs e) {
Paint += new PaintEventHandler(Form1_Paint);
}
private void print(Bitmap image, PaintEventArgs e) {
Graphics graphicsObj = e.Graphics;
graphicsObj.DrawImage(image, 60, 10);
graphicsObj.Dispose();
}
private void Form1_Paint(object sender, PaintEventArgs e) {
Rectangle rect = Screen.PrimaryScreen.Bounds;
PixelFormat pf;
pf = PixelFormat.Format32bppArgb;
Bitmap image = new Bitmap(rect.Width, rect.Height, pf);
Graphics g = Graphics.FromImage(image);
g.Clear(Color.Orange);
BufferedGraphicsContext context = new BufferedGraphicsContext();
BufferedGraphics buffer = context.Allocate(g, new Rectangle(0, 0, rect.Width + 20, rect.Height + 20));
buffer.Render(g);
print(image, e);
}
}
I expect to see orange rectangle on my screen, but it's black. I can't understand why this happen. Help me please :)
buffer.Render(g) renders the contents of the buffer to the graphics object. This means that the orange color is being overwritten by the empty buffer.
You'll have to choose between using BufferedGraphicsContext or creating a buffer yourself (the image).
The following would fix your issue using just the image:
...
Bitmap image = new Bitmap(rect.Width, rect.Height, pf);
using (Graphics g = Graphics.FromImage(image))
{
g.Clear(Color.Orange);
}
print(image, e);
You could also still use BufferedGraphicsContext, but you'd have to write the image to its Graphics property:
print(image, buffer.Graphics); // render your image to the buffer
buffer.Render(e.Graphics); // render the buffer to the paint event graphics
By the way, don't Dispose the graphics object provided by Form1_Paint (you currently do that in the print() method.
As a response to your comment, BufferedGraphicsContext seems to not support transparency when rendering it to the "main" graphics object, but you can draw transparent images to it correctly. The following example shows how the buffer is filled with a red color, and then a transparent image with a blue line is drawn to it:
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
using (BufferedGraphicsContext context = new BufferedGraphicsContext())
using (BufferedGraphics buffer = context.Allocate(e.Graphics, new Rectangle(0, 0, 120, 120)))
{
// Create a bitmap with just a blue line on it
Bitmap bmp = new Bitmap(100, 100, PixelFormat.Format32bppArgb);
using (Graphics g = Graphics.FromImage(bmp))
{
g.DrawLine(Pens.Blue, 0, 0, 100, 100);
}
// Fill a red square
buffer.Graphics.FillRectangle(Brushes.Red, 5, 5, 110, 110);
// Draw the blue-line image over the red square area
buffer.Graphics.DrawImage(bmp, 10, 10);
// Render the buffer to the underlying graphics
buffer.Render(e.Graphics);
}
}
In the result you can clearly see the blue line from the image over the red color in the background buffer (the red background is not overwritten) and there's a black border around the red rectangle where no background pixels where drawn.
I want to capture part of my form and draw it in bitmap variable ...
when I use drawToBitmap() function and set rectangle(12,40,...),the function just capture from 0,0 point of the form.
then what do I do to solve this?
tanks for your help
Bitmap bmp = new Bitmap(((int)maxWidth)+2, ((int)maxHeight)+2);
this.DrawToBitmap(bmp,new Rectangle(0,40,((int)maxWidth)+2, ((int)maxHeight)+2));
Ok so what I did here was created a new form and added a button and a picture box. When you click the button it cuts a rectangle out of the Form and draws it to the picturebox.
I used -100,0 which moves the image 100 pixels to the left.
private void button1_Click(object sender, EventArgs e)
{
//The image we will be drawing on then passing to picturebox
Bitmap bmp=new Bitmap(pictureBox1.Width,pictureBox1.Height);
using (Graphics g=Graphics.FromImage(bmp))
{
using (Bitmap b = new Bitmap(this.Width, this.Height))
{
//captures the Form screenschot, and saves it into Bitmap b
this.DrawToBitmap(b, new Rectangle(0, 0, this.Width, this.Height));
//this draws the image from Bitmap b starting at the specified location to Bitmap bmp
g.DrawImageUnscaled(b, -100, 0);
}
}
//this assigns pictureBox1 the bmp Bitmap.
pictureBox1.Image = bmp;
}