I am using this code to get images from my webcam, I guess its Metricam library
Anyone knows how to get images without using picturebox?
WebCam camera = new WebCam();
camera.Connect();
pictureBox1.Image = camera.GetBitmap();
pictureBox1.Image.Save(#"c:\image1 " + ".Jpeg", ImageFormat.Jpeg);
pictureBox1.Image is Image type. It can handle saving on its own. It does handle saving when used from PictureBox too, notice how you call Save() method on Image property and not on pictureBox instance.
WebCam camera = new WebCam();
camera.Connect();
Image image = camera.GetBitmap();
image.Save(#"c:\image1 " + ".Jpeg", ImageFormat.Jpeg);
Related
I upload a file , after upload base on info i got , i resize image , crop and resize it again
but just first resize work , crop and second resize do noting on image, after first resize what i must to do ?
WebImage img = new WebImage(file.InputStream);
img.Resize(image_sw, image_sh);
img.Crop(image_y, image_x, image_y + image_h, image_x + image_w);
img.Resize(300, 300);
var path = Request.MapPath("~/Content/images/category/" + model.ID + "." + type);
img.Save(path);
The Resize method has a return argument with the new resized image. Just saving the img will save the old image in the old sizes.
Try this:
img = img.Resize(300, 300);
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 am developing a WPF application, a messenger client. The user should be able to change his avatar image. When he right-clicks his avatar, a open file dialog appears and there he can select the image he wants. After he has made his decision I delete his current avatar and copy the new one in the same place with the same name. The avatar image is a rectangle with an imagebrush fill. The problem is that the image does not update until I restart the application. Here is the piece of code used to load the new image.
if (x.ShowDialog() == true)
{
ImageBrush img = new ImageBrush();
BitmapImage bit = new BitmapImage();
Bitmap bmp = new Bitmap(x.FileName);
System.IO.File.Delete(System.IO.Directory.GetCurrentDirectory() + "/data/images/avatar/x.png");
bmp.Save(System.IO.Directory.GetCurrentDirectory() + "/data/images/avatar/x.png", System.Drawing.Imaging.ImageFormat.Png);
bit.BeginInit();
bit.CacheOption = BitmapCacheOption.OnLoad;
bit.UriSource = new Uri(#"pack://siteoforigin:,,,/data/images/avatar/x.png");
bit.EndInit();
img.ImageSource = bit;
ProfileImage.Fill = img;
}
I hope you can help me solve this issue or offer me some alternatives. Thank you all!
In order to make BitmapImage reload the image file (with identical file path), you could set the BitmapCreateOptions.IgnoreImageCache option:
bit.BeginInit();
bit.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
bit.CacheOption = BitmapCacheOption.OnLoad;
bit.UriSource = new Uri(#"pack://siteoforigin:,,,/data/images/avatar/x.png");
bit.EndInit();
img.ImageSource = bit;
I have this code to rotate an image in an if loop in C# Windows Form application, but the Form does not show anything in the form output.
Can anyone help?
this.splitContainer1.Panel2.Controls.Add(PictureBox1);
PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
PictureBox1.Image = bitmap; //Image.FromFile(#"C:\image.jpg");
PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
PictureBox1.Image = (Image)(RotateImg(bitmap, 30.0f, Color.Transparent));
if you need to rotate an image on common angles you can use RotateFlip method with ease.
Please see my sample code:
string fileName = "somefile.png";
System.Drawing.Imaging.ImageFormat imageFormat = System.Drawing.Imaging.ImageFormat.Png;
Bitmap bitmap =(Bitmap)Bitmap.FromFile(fileName );
//this will rotate image to the left...
bitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
//lets save result back to file...
bitmap.Save(fileName, imageFormat);
bitmap.Dispose();
That's all, hope it helps.
Try this:
PictureBox1.Images.RotateFlip(RotateFlipType.Rotate180FlipX);
PictureBox1.Refresh();
I am getting a parameter is not valid exception..While saving the image
Here is my code to save the image
if (!File.Exists(pictureBox1.Tag.ToString()))
{
Image image = pictureBox1.Image;
image.Save(pictureBox1.Tag.ToString(), ImageFormat.Jpeg);
}
else
{
string delStr = pictureBox1.Tag.ToString();
pictureBox1.Image.Dispose();
File.Delete(delStr);
Image image = pictureBox1.Image;
image.Save(delStr, ImageFormat.Jpeg);
pictureBox1.Image = Image.FromFile(delStr);
}
In your else branch, you start by calling Dispose() on your Image, then you try to save it. That can't possibly work, because:
The Dispose method leaves the Image in
an unusable state.
Maybe you wanted to do:
string delStr = pictureBox1.Tag.ToString();
File.Delete(delStr);
Image image = pictureBox1.Image;
image.Save(delStr, ImageFormat.Jpeg);
image.Dispose();
pictureBox1.Image = Image.FromFile(delStr);
But that's still reloading the Image from the very file it has just been saved to, so it's not very useful. You might want to try something like:
string filename = pictureBox1.Tag.ToString();
if (File.Exists(filename) {
File.Delete(filename);
}
pictureBox1.Image.Save(filename, ImageFormat.Jpeg);
But then again, Image.Save() will overwrite the file anyway... So you only need to do:
pictureBox1.Image.Save(pictureBox1.Tag.ToString(), ImageFormat.Jpeg);
Am I seeing correctly? Are you disposing the image before saving it? If so here is your problem:
pictureBox1.Image.Dispose(); // THIS !!!!!! Just remove this line or move it to after save
Image image = pictureBox1.Image;
image.Save(delStr, ImageFormat.Jpeg);