C# - same control on two forms? - c#

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.

Related

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.

Image Source WPF

I have a 2 Image Controls in my WPF Application,
I have a List of Image, I want to assign the images from the List to multiple controls.
imgBackground= t.BackgroundImage //Only shows in one component don't work
imgBackground.Source = t.BackgroundImage.Source ;//don't work
The Image is not showing in all the Image control i try to assign it to.
How can i fix this?
Note t.BackgroundImage and imgBackground is of type System.Windows.Controls.Image

How to prevent PictureBox internally refreshes itself?

I have a very fast loop which renders animation in a Bitmap buffer and adds filter to it (by using LockBits/UnlockBits to access to the raw data and Marshaling changes to it.) in an independent thread.
I wanted to figure out a way to display the render on the Form, real-time, so I created a PictureBox and linked its Image to the bitmap I created. Everytime immediately after the bitmap is unlocked, I refreshed the PictureBox (using delegate, to do cross-threading) so that the rendering is updated properly.
It's totally fine and works very fast, but one big problem came out as I tried dragging the form to the border of the screen, to see if any bug would appear, and oops, the app collapse..saying 'the bitmap is being locked' This happens when either there's other window moving above the PictureBox or the PictureBox is dragged partially out of the screen. I suspice it because PictureBox can refresh itself when redraw is neccessary, and it does when the bitmap is still being locked. So...any way to sovle this problem? Or anyother ways to render the my animation better?
One of possible solutions could be is create your custom MyPictureBox : PictureBox (say) class which override OnPaintBackground, like this:
protected override OnPaintBackground(...)
{
// nothing, an empty method
}
But I'm not very sure that this will work, you should to check this by yourself.
What I would do, personally, considering your comment:
I have a very fast loop which renders animation in a Bitmap buffer and
adds filter to it (by using LockBits/UnlockBits to access to the raw
data and Marshaling changes to it.) in an independent thread
just forget about PictureBox, cause I found it, personally, too generic and non suitable for high performance rendering. Just write a simple class that handles the drawing of specified bitmap on specified surface.
Imo, this is a best choice.
You can't do that.
Instead, you should copy the image (on the background thread) and put the copy in the PictureBox.
For better performance, you can swap between two images to avoid creating too many images.

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.

How to re-shape form?

I have a form in an application developed using C#. In that form I have created a graphic shape (a circle). At run-time I want my form also to be of that shape only. That is, I want to display only that graphic and not the form back ground or title bar or anything. I want to display only that graphic. But the thing is I'm not able to shape my form. I have that graphic control as a User-Control which I have added to my form.
I suspect you're trying to make a splash-screen like effect. This isn't terribly hard to do. Here's a good tutorial to get you started.
The trick essentially is to set the transparency key of the form to the color you wish to be transparent (in this case, everything except your circle. Additionally, you need to set the form to be borderless.
As an aside, you might edit your question to add some information about why you want to do this - I am curious what your goal is, in terms of user-experience.
You could also check MSDN for the Region property. You can use System.Drawing objects to draw whatever shape you want then set the forms Region property before its shown and it will take whatever shape you give it...heres a short example:
http://www.vcskicks.com/custom_shape_form_region.php
If you want a circular form you can put the following code in the form load event handler:
System.Drawing.Drawing2D.GraphicsPath myPath = new System.Drawing.Drawing2D.GraphicsPath();
//this line of code adds an ellipse to the graphics path that inscribes
//the rectangle defined by the form's width and height
myPath.AddEllipse(0,0,this.Width,this.Height);
//creates a new region from the GraphicsPath
Region myRegion = new Region(myPath);
this.Region = myRegion;
and then set the FormBorderStyle property of the form to None.

Categories

Resources