i want to save a image with out user interaction in C# - c#

I m scanning image in a form i want to save the image with out user interaction in C# can any one help..
string chktemp = #"C:\"+pic+".tif";
Bitmap bm= new Bitmap(from file);//Here it is asking already saved file with out this i want to save just now scanned image from form
bm.Save(chktemp);

Use IO to check if the current file exists, and if so delete it first.
See: System.IO documentation
File.Exists
http://msdn.microsoft.com/en-us/library/system.io.file.exists.aspx
File.Delete
http://msdn.microsoft.com/en-us/library/system.io.file.delete.aspx

Load the Image File to Stream ,than you can Create Bitmap from Stream ,which will close the File Read Operation in source Image .

Related

save and save as in Windows Form App by C#

I'm using this code for making save as of image in c#
SaveFileDialog svf = new SaveFileDialog();
svf.Filter = "JPEG files (*.jpeg)|*.jpeg";
if (DialogResult.OK == svf.ShowDialog())
{
this.imgbox.Image.Save(svf.FileName,ImageFormat.Jpeg);
}
I need to make save now for image without change name or location ( apply save not save as as the above code ) how i can use this code for save now ?
Assuming WinForms, add a field or property to the appropriate control to hold the full path to the file currently opened, and call it currentFileName or something.
When opening a file, assign the path to the variable. Now when the user hits the Save menu, you can use the already stored path to overwrite the opened file. You can add a Save As menu that pops up the SaveFileDialog, and afterwards assigns the result to the variable again - if not cancelled.
When the user invokes the New or Close menu, make sure to clear the stored path.

How can I change an image/bitmap that is already in use?

I have a directory with .png images which I display in a third party combobox of my c# program. So the user is able to choose one of this images using the combo box. Basic code used:
Bitmap thump = new Bitmap(<path>);
ComboItem item = new ComboItem();
item.Image = thump;
MyComboBox.Items.Add(item);
Now I would like to update one of this images at runtime. Unfortunately I can't delete the old image because it is still opened in my program, so somehow I either need to close it or open it in a way that does not keep the image in use by my program. The changes to the bitmap are not done in my program, I just pass the path of the dirctory to another program which saves the bitmap there (but fails at the moment because it can't delete the old bitmap).
I guess this is a simple problem but I could not find a solution here or on the internet.
First read the file to memory, then create the Bitmap using that data.
var m = new MemoryStream(File.ReadAllBytes(filename));
Bitmap thump = (Bitmap)Bitmap.FromStream(m);

Resource file modification date

I am getting an image from a resource file like this:
Image img = (Image)Resources.ResourceManager.GetObject(imgName) as Image;
Is there a way to get the modification date of this image?
Sorry, you won't be able to access that information as the image is loaded into the assembly as binary information, not as a file.

C# :How can i set Memory stream as the asp.net image control's source property?

I have an asp.net image control in one ASP.NET page and have a Memory Stream which has an image.How can i convert this memory stream to set it as the image source without storing the image in the hard disk ?
The image control takes an ImageUrl - a path to where the image is located.
There is no property that takes an actual image (or image data).
What you can do is write a HttpHandler that will stream your images from whatever source - set the ImageUrl to use this handler.
Here is an example for a general file handler using a memory stream - it's a good starting point for what I suggest.
you'll have to either write that file onto disk or make another page (action on the same page?) that writes the content of your MemoryStream into the Response, and then point the image control to that source.

Add Image to Base64 Encoded ImageStream in Resources (resx)

I'm working on an older project, updating it. Part of the program has a toolstrip with many buttons on it, each with an image. I found that the images are stored in a Base64 encoded imagestream in the resx for the form and accessed as such:
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
...
this.imageList1 = new System.Windows.Forms.ImageList(this.components);
...
this.toolStrip1.ImageList = this.imageList1;
...
this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
...
this.toolStripButton1.ImageIndex = 0; //There are 41 images, so this can be between 0 and 40
I need to add another button with a new image. How can I add an image to this stream?
I cannot use the designer, as it crashes as soon as I load the form (I believe because it uses a custom component with unsafe code).
I could always just add a new image resource separate from the stream, but that would make that one button different and thus it would create problems with consistency, causing upkeep problems later. So I'm wondering if there is any way for me to edit the imagestream. I can access the raw base64 string, but I have no idea where to go from here.
Create another form.
Add an ImageList component.
Add an arbitrary image in order to generate an "imagestream".
Open old resx and copy the "value" element.
Open new resx and paste value element.
Open new form.
Add images as needed.
Save new form.
Open new form's resx file.
Copy value element.
Open old form's resx file.
Paste in new value element.
I found a way to do this using code:
imageList1.Images.Add( NEWIMAGE );
ResXResourceWriter writer = new ResXResourceWriter("newresource.resx");
writer.AddResource("imageList1.ImageStream",imageList1.ImageStream);
writer.Generate();
writer.Close();
writer.Dispose();
The code will write the updated ImageStream to the new resource file. I can then copy it to my current resource file.

Categories