How do I generate an Image through C# code? - c#

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.

Related

Displaying editable image in 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.

Load 2 images in 1 picturebox - C#

How can I load 2 Images in one PictureBox ?
Here is an example:
http://postimg.org/image/l78kth897/
Thank you so much.
You can use Graphics.DrawImage() to draw any image anywhere inside a PictureBox or any other control for that matter. If you're writing your own control, override OnPaint(). If you want to use an existing PictureBox, simply use its Paint event to do this:
e.Graphics.DrawImage(YourImageObjectHere, ...);
e.Graphics.DrawImage(YourSecondImageObjectHere, ...);
GDI+ already supports transparency channel, so if your images have transparent areas, they'll draw just like the sample image you have posted. DrawImage() has over a dozen overloads, using which you can control several aspects of how an image is drawn. The simplest one takes the image object and the position to draw at.
Remember that an image object is an object of System.Drawing.Image or one of its derived classes. If all you have is the path of the image, you should use Image.FromFile() to create an Image object from that image file first.

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.

How to create an Image object from an Icon for use with ToolStripStatusLabel items

I need to be able to set an icon on a ToolStripStatusLabel item in a C# Windows Forms application. The item will being displaying an image and text. I assumed that I could use an Icon with such an item but I am mistaken. What is the best way of creating an Image object from an existing Icon object?
the intended code will look something like:
Image image = new Image(); // Get this image somehow from a pre-existing Icon object
serverInfoToolStripStatusLabel.Text = "Connected to server X";
serverInfoToolStripStatusLabel.Image = image;
(NB: serverInfoToolStripStatusLabel is declared as System.Windows.Forms.ToolStripStatusLabel)
regards.
Icon has the ToBitmap method, which will give you a Bitmap, which is an Image
You can set the image directly in designer.
If you have to use code, why not add the image to your project Resources and then assign the image property to it?

C# - same control on two forms?

I have extended a PictureBox and created a singleton.
Is it possible to display the same instance of a PictureBox control on two distinct form same time ?
Thanks in advance.
No - a control has a single parent control.
Of course not. Each Control has Parent property (and underlying window has parent window). Control has to communicate with its parent.
Having said that, if you need ImageControls on different forms to display the same image, here's what you can do.
Approach A
1) Create a (global) list of these picture boxes in your application.
class Globals //or whatever
{
public static List<PictureBox> allBoxes=new List<PictureBox> ();
2) On form creation add each PictureBox to this list.
foreach (Control c in Controls)
{
PictureBox pb = c as PictureBox;
if (pb != null) Globals.allBoxes.Add(pb);
}
3) when you need to change the image, iterate through this list:
foreach (PictureBox p in Globals.allBoxes)
{
p.Image=myImage;
}
I tested it a little, and it seems that you don't need to clone the image.
Approach B
1) Choose one 'master' PictureBox in your application.
2) Subclass PictureBox, so that it fires an event ImageChanged whenever Image property is set (some code samples in this thread)
3) On every other form having PictureBoxes, add an event handler to ImageChanged event of that 'master box' (masterBox.OnImageChanged+=new EventHandler(ImageChanged);
4) In the handler change all images
I prefer approach A.
I'm guessing you want to show the same picture in both pictureboxes? Take a look at the help file (take particular interest in the NOTE section).
PictureBox help
Remarks
Typically the PictureBox is used to
display graphics from a bitmap,
metafile, icon, JPEG, GIF, or PNG
file.
Set the Image property to the Image
you want to display, either at design
time or at run time. You can
alternatively specify the image by
setting the ImageLocation property and
load the image synchronously using the
Load method or asynchronously using
the LoadAsync method. NoteNote:
If you want to use the same image in
multiple PictureBox controls, create a
clone of the image for each
PictureBox. Accessing the same image
from multiple controls causes an
exception to occur.
If your are trying to display a Logo on each form then I would subclass the Picturebox and just load the image into it. No singleton, no magic.
But be carefull: You have to load the Image for each PictureBox. From MSDN:
http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.image.aspx
The Image property is set to the Image
to display. You can do this either at
design time or at run time.
Note: If you want to use the same
image in multiple PictureBox controls,
create a clone of the image for each
PictureBox. Accessing the same image
from multiple controls causes an
exception to occur.

Categories

Resources