I have write below codes in c# to display an image in PictureBox but when run the application, nothing shown...
Please help me to fix that.
here is my code:
private void button1_Click(object sender, EventArgs e)
{
PictureBox p =new PictureBox();
p.ImageLocation = "1.jpg"
p.Location = new Point(100, 75);
}
Add this line:
this.Controls.Add(p);
PictureBox.Image = new Bitmap("yourImage.jpg");
The formats supported
are: BMP, EMF, EXIF, GIF, ICON, JPEG, PNG, TIFF and WMF.
It's possible that the PictureBox size is small and your image is too big (check the SizeMode property to "StrechImage")
I was using a high res PNG icon with transparent background and took a little time to figure out.
Related
In my c# i use CopyFromScreen
On many pc's the script ar always ok,
the panel1 in my Form will be saved as image
but when i use touchdisplays in the image is evrything but newer the panel1
i can not understand why?
private void buttonInsert_Click(object sender, EventArgs e)
{
Image bmp = new Bitmap(panel1.Width, panel1.Height);
var gg = Graphics.FromImage(bmp);
var rect = panel1.RectangleToScreen(panel1.ClientRectangle);
gg.CopyFromScreen(rect.Location, Point.Empty, panel1.Size);
bmp.Save(#"C:\Temp\test.jpg", ImageFormat.Jpeg);
Graphics g = panel1.CreateGraphics();
g.Clear(Color.WhiteSmoke);
}
thx for your help
I found it out, the problem was the windows scaling. When i set it on 100% the Problem solved.
thx a lot
I need to save the whole of a DataGridView as an Image.
I have seen some post online but it did not work for me.
So far, I've tried these 2 links:
DataGridView to Bimap and Save Image in folder.
What I intend is that once a Button is pressed, the DataGridView will be converted into an Image and it will be automatically saved to the Desktop.
The code I'm using generates an error:
A generic error occurred in GDI+
private void button1_Click(object sender, EventArgs e)
{
//Resize DataGridView to full height.
int height = dataGridView1.Height;
dataGridView1.Height = dataGridView1.RowCount * dataGridView1.RowTemplate.Height;
//Create a Bitmap and draw the DataGridView on it.
Bitmap bitmap = new Bitmap(this.dataGridView1.Width, this.dataGridView1.Height);
dataGridView1.DrawToBitmap(bitmap, new Rectangle(0, 0, this.dataGridView1.Width, this.dataGridView1.Height));
//Resize DataGridView back to original height.
dataGridView1.Height = height;
//Save the Bitmap to folder.
bitmap.Save(#"C:\\Desktop\\datagrid.jpg");
}
Hope to get some help. Thanks!
You need to fix more than one section of you code:
bitmap.Save(#"C:\\Desktop\\datagrid.jpg");. This path string should be:
#"C:\Users\SomeUser\Desktop\datagrid.jpg"
or
"C:\\Users\\SomeUser\\Desktop\\datagrid.jpg"`
See point 6.
When calculating the DataGridView height, you're not including the grid's Header.
When creating a Bitmap object, that object must be disposed of, as any other disposable object you create. You can use the Bitmap.Dispose() method or declare your object with a using statement (preferred).
Bitmap.Save([Path]), without specifying an ImageFormat, creates a PNG image. The file extension you insert in the Path string is not considered. At this time, you're creating a file with a .jpg extension when it's actually a .png file.
You should use the Png format, not Jpeg when saving this kind of bitmap. Its loss-less compression is more adeguate: it will preserve the image colors and improve the overall quality.
The Path to the current user Desktop should not be hard-coded. This path is returned by Environment.SpecialFolder.Desktop.
You could modify your code as follows:
using System.IO;
private void button1_Click(object sender, EventArgs e)
{
int DGVOriginalHeight = dataGridView1.Height;
dataGridView1.Height = (dataGridView1.RowCount * dataGridView1.RowTemplate.Height) +
dataGridView1.ColumnHeadersHeight;
using (Bitmap bitmap = new Bitmap(dataGridView1.Width, dataGridView1.Height))
{
dataGridView1.DrawToBitmap(bitmap, new Rectangle(Point.Empty, dataGridView1.Size));
string DesktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
bitmap.Save(Path.Combine(DesktopFolder, "datagridview1.png"), ImageFormat.Png);
}
dataGridView1.Height = DGVOriginalHeight;
}
try this
image save file path , like this "C:\Users\User\Desktop\datagrid.jpg"
bitmap.Save(#"C:\Users\User\Desktop\datagrid.jpg");
Try Replacing bitmap.Save(#"C:\\Desktop\\datagrid.jpg"); with :
File.WriteAllBytes(#"C:\\Desktop\\datagrid.jpg", (byte[])new ImageConverter().ConvertTo(bitmap, typeof(byte[])));
I'm a c# beginner and I'm wondering if there is a way that I could draw 2 images from 2 different pictureboxes and save it as a one image using the method DrawToBitmap.
I can preview everything nice in the program itself (it looks good), but the main problem is when I save the picture, It's just showing the picturebox1 with a blank-alike icon of the picturebox2 in the middle :/
Here's the part of my code and it's not working as it should
pictureBox2.ImageLocation = potDoSlike;
Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
pictureBox1.DrawToBitmap(bmp, pictureBox1.Bounds);
pictureBox2.DrawToBitmap(bmp, pictureBox2.Bounds);
bmp.Save(#"D:\asd.jpg");
I figured it out! The problem was that I didn't save anything to the pictureBox.Image and it couldn't draw nothing out... well here's the code!
pictureBox2.Parent = pictureBox1;
pictureBox2.ImageLocation = potDoSlike;
pictureBox2.Image = Image.FromFile(potDoSlike);
Bitmap bmp = new Bitmap(pictureBox1.Image);
pictureBox1.DrawToBitmap(bmp, pictureBox1.Bounds);
pictureBox2.DrawToBitmap(bmp, pictureBox2.Bounds);
bmp.Save(#"D:\asd.jpg");
I have a timer tick event set to 250ms:
private void timer4_Tick(object sender, EventArgs e)
{
if (pictureboximagestosavecount == 72)
{
timer4.Enabled = false;
}
else
{
Bitmap bmp = new Bitmap(this.Width, this.Height);
Rectangle rect = new Rectangle(this.Location.X, this.Location.Y, this.Width, this.Height);
pictureboximagestosavecount++;
savePictureBox(pictureBox1, #"c:\temp\pboximages\" + pictureboximagestosavecount.ToString("D6") + "pbimg.gif");
this.DrawToBitmap(bmp, rect);
bmp.Save(#"c:\temp\pboximages\" + pictureboximagestosavecount.ToString("D6") + "form.gif");
}
}
First I'm saving the pictureBox as gif using the method savePictureBox.
Second I save the form1:
bmp.Save(#"c:\temp\pboximages\" + pictureboximagestosavecount.ToString("D6") + "form.gif");
After timer4 is stopped I have abutton click event where I create animated gif from the saved files:
private void animatedgifbutton_Click(object sender, EventArgs e)
{
DirectoryInfo di1;
FileInfo[] fi1;
di1 = new DirectoryInfo(#"c:\temp\pboximages\");
fi1 = di1.GetFiles("*form.gif");
List<string> newImages = new List<string>();
for (int i = 0; i < fi1.Length; i++)
{
newImages.Add(fi1[i].FullName);
}
animatedgif.MakeGIF(newImages, #"c:\temp\pboximages\animated1.gif", 6, true);
}
When I'm doing *.form.gif and I see in the List newImages only the form gifs files the animatedgif.MakeGIF throw error since it need to get List of gifs files but I guess that when I save the form it's saving it as bitmap and not real gif.
How can I take a screenshot of form1 and save it as real gif ?
When I save the pictureBox1 to the hard disk it is GIF and there are no problems.
The problem is with saving the form.
EDIT:
I also saw now that the way I'm saving a screenshot of the form1 is not good it's not saving the whole form area only part of it. I gues using this bmp and rect is not good.
This is what I get for example when I'm saving a screenshot of the form:
In other words I need in timer4 tick event to save the form screen the whole form to the hard disk as a gif file real gif file not bmp.
It doesn't save as a proper GIF because you don't tell it to. The extension is not enough, you need to add the ImageFormat to the Save call!
And it doesn't save the whole area of the Form because you don't use the right Rectangle to control it.
Both issues go away very simply if you use the modified savePictueBox function:
void saveControl(Control Ctl, string fileName)
{
Rectangle Rect = Ctl.ClientRectangle;
// if (Ctl is Form) Rect = Ctl.Bounds; // (*)
using (Bitmap bmp = new Bitmap(Rect.Width, Rect.Height))
{
Ctl.DrawToBitmap(bmp, Rect );
bmp.Save(fileName, System.Drawing.Imaging.ImageFormat.Gif);
}
}
Note that this uses the ClientRectangle, that is, it leaves out the Form border. If you want if to include the Border you can uncomment the line (*)
Now you can call it as
saveControl(pictureBox1, yourFileName);
and as
saveControl(this, yourOtherFileName);
Two notes on Animated Gifs:
Since you are recording the Gifs in real-time don't try to create the animation at the same time!
I found this post interesting. fireydude's answer does work, once you have included all those references from the WPF world..Not sure how to get the best quality and unfortunately there is no control over the timing, so far..
I have a picturebox with a png in it. Yet even when I set the BackColor to Transparent, it is not transparent. Any ideas what might be wrong? :)
Thanks!
I have also faced the problem regarding transparent pictures.
you have to Draw it through code.
See my question A PictureBox Problem
EDIT:
In paint event (Control containing Background Image)
write this
//If added your image in project's resources get from there OR your Image location
Image img = yourNamespace.Properties.Resources.yourPicture;
e.Graphics.DrawImage(img,50,50,100,100);
Your PNG file should also have the transparent background. This is can be done while creating the image(png) files.
You really have to draw it through code. Place a pictureBox on your form, set sizeMode and docking as you like. Then you may fire the following function on the pictureBox's PAINT event:
public void LogoDrawTransparent(PaintEventArgs e)
{
// Create a Bitmap object from an image file.
Image myImg;
Bitmap myBitmap;
try
{
myImg = cls_convertImagesByte.GetImageFromByte(newImg);
myBitmap = new Bitmap(myImg); // #"C:\Temp\imgSwacaa.jpg");
// Get the color of a background pixel.
Color backColor = myBitmap.GetPixel(0, 0); // GetPixel(1, 1);
Color backColorGray = Color.Gray;
Color backColorGrayLight = Color.LightGray;
Color backColorWhiteSmoke = Color.WhiteSmoke;
Color backColorWhite = Color.White;
Color backColorWheat = Color.Wheat;
// Make backColor transparent for myBitmap.
myBitmap.MakeTransparent(backColor);
// OPTIONALLY, you may make any other "suspicious" back color transparent (usually gray, light gray or whitesmoke)
myBitmap.MakeTransparent(backColorGray);
myBitmap.MakeTransparent(backColorGrayLight);
myBitmap.MakeTransparent(backColorWhiteSmoke);
// Draw myBitmap to the screen.
e.Graphics.DrawImage(myBitmap, 0, 0, pictureBox1.Width, pictureBox1.Height); //myBitmap.Width, myBitmap.Height);
}
catch
{
try { pictureBox1.Image = cls_convertImagesByte.GetImageFromByte(newImg); }
catch { } //must do something
}
}
This is my class that is referenced in the above function:
class cls_convertImagesByte
{
public static Image GetImageFromByte(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage;
}
public static byte[] GetByteArrayFromImage(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}
}
Thanks. chagbert.
From what I learned I can't do it within a windows form, as it doesn't have layers for the images. So guess will have to make it as WPF. :)
How did you create the background? Is that set by setting the Form.BackgroundImage?
If that background (the paper like image) is a container control, the transparency should just work.
However, If you are placing two PictureBox objects on top of eachother this doesn't work. The transparent area takes on the color of its parent object. If you have two PictureBox objects they both will have the Form as their parent. If this is your situation, it can be solved by setting the transparent image's Parent property to be the background image.
private void Form1_Load(object sender, EventArgs e)
{
transparentPictureBox.Parent = backgroundPictureBox;
}
When changing the Parent property, the Location of the tranparentPictureBox will become relative to its new parent. You'd have to subtract the x and y coordinates of the background image from the transparent image. See my answer on A PictureBox Question for an example with a screen shot.
AFAIK, you can not set the Parent in the Designer, only in code. Therefore, the Designer will still not show a transparent image, but at runtime it should.
The same problem occurs if you place a transparent Label on top of a PictureBox object.