Displaying editable image in c# - c#

I want to display an image (.tif, gray value 16 bit) which is editable for the user via sliders. The displayed image should directly react to the changes, so the user knows what he's doing to the image.
Currently I only seem to be creating new files with every change and displaying the latest one, which is a terrible solution.
My idea would be to load the original pixeldata, put it in some sort of temporary file which isn't and won't be saved, then saving the parameters of the sliders and when hitting save applying the parameters to the original image (since in the end the filter is used on an intire set of images).

An Image is not a File. If might be that you display the Image that is in a file, but once you've loaded the file into an Image object don't ever use the file again, until the edited image needs to be saved in a file.
You say you want to display an image. As you are using winforms, I assume you are using a PictureBox control. If not, you have a class that you order to display an Image.
Image imageToDisplay = GetImageToDisplay();
this.PictureBox1.Image = imageToDisplay;
GetImageTodisplay will read the file and return it as an Image
System.Drawing.Image GetImageToDisplay()
{
FileStream imageStream = new FileStream(this.GetImageFileName(), FileMode.Open);
System.Drawing.Image image = new Image(imageStream);
return image;
}
Apparently you also have sliders. You don't mention the class of your sliders, but apparently it is an object that notifies your form about a new slider value.
void OnSliderValueChanged(object sender, int sliderValue)
{
Image displayedImage = this.PictureBox1.Image;
this.ProcessSliderChanged(image, sliderValue);
}
Until now I don't see the creation of a new file. So this should be in ProcessSliderChange. Luckily that is a function you created yourself. All you have to do is change the image according to your new slider value. Don't do anything with file handling and you won't get a new file
void ProcessSliderChange(System.Drawing.Image image, int sliderValue)
{ // what do you want to do with the image?
// make it darker? move it? change contrast?
}
Unfortunately the Image class doesn't have a lot of functions to edit images, I assume you have a library with functions that act on System.Drawing.Image objects. Just use those functions to change your Image.

Related

Images Scaling Down in draw in C#

Alright guys last little bit of this project I'll ask for help on I promise.
So I go to load the images, works fine however I notice upon loading that the dimensions of the image have been scaled down in the y to 300 (all are a constant value of 433) and up or down from their original width to 600.
I'm using the following method to load them
foreach (string file in Directory.EnumerateFiles(imagePath, "*.JPG"))
{
Image contents = Image.FromFile(file);
treesImage[count] = contents;
count++;
}
and this is the resulting image when I have it loaded.
http://i.stack.imgur.com/Q40kK.png
As you can see the image below the red rectangle is quite small
Any help would be appreciated. If you require any more information please post below and I'll make sure to edit the original question with the relevant information as soon as humanly possible.
EDIT: I am using a simple windows form application and not another graphical framework for my own reasons.
Thanks in advance :)
I'll assume you are using a PictureBox control to display the image.
When someone chooses a tree from your map, you obviously set the PictureBox Image property to the image object referenced by the index in the array. Use the Image object to set the ClientSize of the PictureBox control.
...
Image img = treesImage[idx];
MyPictureBox.SizeMode = PictureBoxSizeMode.Normal;
MyPictureBox.ClientSize = new Size(img.Width,img.Height);
MyPictureBox.Image = img;
...
Alternately you can define one size for your PictureBox and force all the images to be scaled to that size by setting the control SizeMode property to StretchImage declaratively.
I would recommend that you create a simple class (MyImageInfo for example) that would store the Path, Width, and Height of the images found in your first function into a list and then just as before when a user clicks to view an image you set the width and height of the PictureBox and then call the LoadAsync(path) method to get the image. then you aren't storing all images in memory at once, just as you need them since it doesn't look like this requires a lot of quick jumping from image to image.

c# Image from file close connection

I display an image using c# in a pictureBox. Then I want to change the image's name. I can't because I get that the image is being used by another process.
I open the image this way
Image image1 = Image.FromFile("IMAGE LOCATION"); ;
pictureBox1.Image = image1;
Then try to change it's name this way and I get an IO exception saying " The process cannot access the file because it's being used by another process.
System.IO.File.Copy(#"OldName", #"NewName"); //copy changes name if paths are in the same folder
Isn't the object image1 holding the image? Why is the file still locked by the previous process? Any suggestions would be appreciated. Many thanks!
Have a look at this question: Free file locked by new Bitmap(filePath)
To free the lock you need to make a copy of the bits:
Image img;
using (var bmpTemp = new Bitmap("image_file_path"))
{
img = new Bitmap(bmpTemp);
}
Yes, picture box control locking IO file on the disk in that way.
If you want to rename linked file, you can:
or dispose picture box, and after rename the file (if this is suitable for your app)
or assign to the picture box a copy/clone of the image.
You can try this:
Image image1 = null;
using(FileStream stream = new FileStream("IMAGE LOCATION", FileMode.Open))
{
image1 = Image.FromStream(stream);
}
pictureBox1.Image = image1
;
you need to dispose your image:
Image image1 = Image.FromFile("IMAGE LOCATION"); ;
pictureBox1.Image = image1;
image1.Dispose();
the do the move.
this solution solved my problem. I was using a picturebox and loading its image directly from the disk without an intermediate image file. when I was done I was doing a "picturebox.dispose" and that did not free up the image file and I couldn't delete it because "file in use by vshost". I had to explicitly create an image variable (IE dim MyImage as image) and then first load the image into the image variable, then load MyImage into the picturebox. by having a reference to the image I could do a MyImage.dispose. That closed the link between vshost and the image and I was able to programmatically delete it.

Convert SWF to PNG image with transparency

I'm converting a swf file to png image using SWFtoImage tool in c#. I'm able to convert the file successfully, but the problem is transparency. The converted image contains a black background instead of transparent, as you can see in the attached image.
how can I fix this? since the tool fails to create a transparent image, is it possible in .net to manipulate on the converted png image and make its background transparent?
Current Code:
//Namespace: BytescoutSWFToVideo;
// Create an instance of SWFToVideo ActiveX object
SWFToVideo converter = new SWFToVideo();
// Register SWFToVideo
converter.RegistrationName = "demo";
converter.RegistrationKey = "demo";
// set input SWF file
converter.InputSWFFileName = #"D:\image2069.swf";
// Enable trasparency
converter.RGBAMode = true;
// Extract all frames to .\Output sub-folder
converter.ConvertAllToPNG(#"D:\Result\");
This is the swf file.
I don't know much about SWFToImage. But the company's web site contains demo code with the following comment (translated to C#):
// Enable trasparency - set BEFORE setting input SWF filename
converter.RGBAMode = true;
So possibly you have to change the order of your statements.

How do I generate an Image through C# code?

I've got a button in my WinForms plugin. It's got an image on it. The thing is, when I click the button, I want the image to change from whatever image it is now, to whatever image I want it to be.
So basically how do i make button change its image? The image type is System.Drawing.Image.
In order to change a Button's image in WinForms you set it's Image property. You can either load a bitmap from a file with Bitmap.FromFile, or you can draw an image using the Graphic class by creating a new instance of Bitmap using the Width/Height constructor (i.e. new Bitmap(100, 100)) and then use the Graphic.FromImage method. Lookup the documentation on the Graphic class for more info.

Display a single frame from a multipage TIFF file from a listbox in a picture box

I have created a C# application which allows you to load a multipage TIFF file and displays them in a listbox. There are then buttons that you can add and remove pages and save the new image file.
I have used the code from here Adding frames to a Multi-Frame TIFF and just changed a few bits so that it suits my application.
The problem is that the images are not legible as the size of the loaded image is too small.
What I would like to do is click on the listbox image and have it display in a larger size in a picture box (or another similar option if better) - allowing the user to decide if they want to remove it...
When I have looked in to this the picture box seems to want you to name a file - as the file is the whole multipage image. This isn't right.
Either that or if I can display the images in the listbox in a legible format - at the moment as the multipages that are being loaded in are not always the same size the pages look squashed..
Please let me know if you need clarification on this question.
I've figured it out - this piece of code is already there:
private void listBox_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
if (this.listBox.Items.Count < 1)
return;
ImageItem i = (ImageItem)this.listBox.Items[e.Index];
e.Graphics.DrawImage(i.Image, e.Bounds, 0, 0, i.Image.Width, i.Image.Width, GraphicsUnit.Pixel);
}
So I linked up the listBox_Click,
private void listbox_Click(object sender, EventArgs e)
{
ImageItem i = (ImageItem)this.listbox.Items[listbox.SelectedIndex];
pictureBox.Image = i.Image;
}
so I took the object and put it in the picture box.

Categories

Resources