Use Button Click To Send Image To New Page. WPF C# - c#

How do you use a button to send an image within the same page to another page in the application via the button click event?

In the method that is called on the button click, I'd use the application properties to store your image. Something like this (you will want to put a data type on your image):
Application.Current.Properties.Add("myImage", System.Windows.Media.Imaging.BitmapImage myBmp);
Window newWindow = new Window();
newWindow.Show();
Then on the window that opens, grab the image from the app properties like this (note that you'll want to cast your image as they image type you want):
transferredImage = (MYIMAGETYPE)Application.Current.Properties["myImage"];
var brush = new ImageBrush();
if (image != null)
{
brush.ImageSource = image;
btnCustomerPhoto.Background = brush;
}
Application.Current.Properties.Clear();
Of course, you don't have to clear the current properties if you don't want to, but I do to ensure I don't have anything sticking around I don't want.
There are other ways to do this but the application properties are easy to use and then get rid of.

Related

User Control: how to add background image on button in setter/getter

In user control designer I know how to add background image.
My image (16x16px) is located in user control. How to add this image in code?
How do I embed the image in user control so that when I bring the control to another project, the image also appears?
private Bitmap buttonResetImage = null;// <-- here I want to load my initial image.Then the user can change this image as he wishes;
public Bitmap ButtonResetImage
{
get { return buttonResetImage; }
set
{
buttonResetImage = value;
btnReset.BackgroundImage = buttonResetImage;
btnReset.BackgroundImageLayout = ImageLayout.Center;
}
}
Well since MDP does not respond I found the trick. I need to initialize this:
private Bitmap buttonResetImage = global::SliderControl.Properties.Resources.Reset;
If I use only:
private Bitmap buttonResetImage = SliderControl.Properties.Resources.Reset;
I get this error:
Error CS0117 'SliderControl' does not contain a definition for 'Properties' SliderControl
https://i.imgur.com/az52MOP.png
In order to create a user control that has any kind of resources like :
images
fonts
audio
icon
and etc, you can create them in a separate project (called Windows Forms Control Library) and add resources in it.
after all, the result(in this case, a dll file) contains all the resources and code base you need.you can use it in any where you want.

Change BackgroundImage of form from Custom Control running in Panel

My project consists of a form with a panel that contains a custom control.
In this custom control I have buttons that change the background image.
My issue is; these buttons only change the background image of the custom control that they are placed in, and I need them to change the background image of the main form containing the panel with the custom control.
My current code:
this.BackgroundImage = Image.FromFile(System.IO.File.ReadAllText(BackgroundSkinsPath));
I need something that will in effect accomplish this:
MainForm.BackgroundImage = Image.FromFile(System.IO.File.ReadAllText(BackgroundSkinsPath));
ie: Change background image of MainForm.cs from CustomControl.cs
You can use Control.FindForm method for that, like this
this.FindForm().BackgroundImage = ...
I ended up using something different:
Form MainForm = Application.OpenForms["(The name of the form in which I wanted to change the background)"];
//...
MainForm.BackgroundImage = Image.FromFile(System.IO.File.ReadAllText(BackgroundSkinsPath));
It ended up being a lot simpler than I thought it was.
Find the control's parents and change the BackgroundImage:
if(this.Parent!=null && this.Parent.Parent!=null)
this.Parent.parent.BackgroundImage = Image.FromFile(System.IO.File.ReadAllText(BackgroundSkinsPath));

Making it so I can close a pic once opened

I have C# code linked to an xaml file that builds a GUI. In the GUI, when I click on an option, the photo appears, as I want. However, I can't figure out how to make it so the user has the option to close the photo. Here's my C# code:
public void help_click(object sender, RoutedEventArgs e)
{
Image img;
img = new Image();
Uri diagram = new Uri(#"pack://application:,,,/PTDGUI;component/Content/Icons/controlmap.png", UriKind.RelativeOrAbsolute);
img.Source = new BitmapImage(diagram);
canvasSpace.Children.Add(img);
}
Image is not based on Control. You may require to customize for this behavior by either writing custom control or trapping any other control events to close this image.
This discsussion(stackoverflow) explains both the options I've mentioned, should help in understand and implementing what you rquired.
you can use the same help button for both, initially button will display "Help", When user clicks help, load the image and change the button text to "Close" and toggle it back to "Help" when users clicks again.
if(button.Text ="Help")
{
---load image
button.Text = "Close";
}
else
{
--Clear image
button.Text ="Help"
}
you may also use bool flag instead of checking text.

Issue designing code to manage objects

I have a C# winform which displays snapshots from a camera. The form has four PictureBox controls and When an image is taken it is placed into pictureBox1 and the previous images are bumped along to 2,3 and 4. Under each picture box is also a label which displays the time stamp and the order number (each image is given a number 1-4, that stays with it until it is bumped off in which the newest image takes that number). Currently I am doing it like below. However I feel this is very inefficient and will cause me problems later on if I decide to add key down events to change the backcolors of some of the labels (to indicate status).
Does anyone know of a better way to do this?
if (count > 4)
{
count = 0
}
count ++;
pictureBox4.image = pictureBox3.image;
pictureBox3.image = pictureBox2.image;
pictureBox2.image = pictureBox1.image;
pictureBox1.imagelocation = (#"http://192.168.X.X/image.cgi")
label4.Text =label3.text;
label3.text = label2.text;
label2.text = label1.text;
label1.text = count.ToString()+ " " + datetime.now();
I could create a new Control, most likely a Panel that contains all of these UI elements in it (PictureBox, Label, anything else). Have a constructor for your Control that takes a URL of the image. Load the image into your PictureBox, and set your label.
Have all of that logic encapsulated in your Control. So when a new one is added, you just create the new Control, and remove the last one in your row, and move the .Left properties of the 3 remaining to their new locations.
Don't forget to implement IDisposable, and Dispose of the Controls when they're removed to free up the resources of displaying the images.
EDIT
If it's not there already, you can provide references back to the top Control in each of your inner Controls (PictureBox and Label), and even to your main form in your top Control by passing this as a parameter in the constructor as well and setting a private member variable inside those controls. That way, when someone clicks on the PictureBox, you can go up the line to this.Parent and get your outer Control. You could even have that reference to your Main Form (hopefully a Panel in there that holds your 4 of these objects). That could be this.Parent.Parent to call a method on there. (I think there's already a public property of Parent on all Controls, so that's fine.)
A little bit of quick coding:
You have your main Form (mainForm). In there is a Panel (picturePanel). picturePanel holds your 4 new Panels, which we'll call customPanel. Each customPanel has a PictureBox (imageBox), and Label (fileNameLabel).
Your customPanel constructor would look like this:
public partial class CustomPanel : Panel {
private PictureBox _imageBox;
private Label _fileNameLabel;
public CustomPanel() {} // This is most likely tied into the code behind file. Sorry, It's been a while since I've done WinForms
public CustomPanel(string imageFileName, Panel parent) {
// Set the source for the PictureBox.
// Set the Text of the label.
_parent = parent;
}
}
Continue with this down the line through the PictureBox and Label. Then in your events, you have your PictureBox work up the chain. To find picturePanel. If you want to get really fancy, you could have that derive from Panel as well and just add a public property that handles all of the switching around of which customPanel sent the message.
So down in your PictureBox event, you could have a line of code like this:
if (this.Parent.Parent is PicturePanel) {
((PicturePanel)this.Parent.Parent).RemovePicture(this.Parent);
}

WPF window content as content of another window

i need to show my window as inside Tab of another window and i do it using the following
var childWindowEditor = new MainWindow(); //window to embed
object content = childWindowEditor.Content; //take out the content of the window to embed
childWindowEditor.Content = null;
EditorTab.Content = content; //just a tab in a tabbed group
its working fine, but the problem is what if i wanted to access the functions of the window class?
i know tha i should create it as user control, but is there a solution without having to create the content of that window as a user control?
would casting the content to the window that i got the content from work?
You should use the MVVM pattern. Your ViewModel would be the DataContext of childWindowEditor. So you could simply do something like
EditorTab.DataContext = childWindowEditor.DataContext;
And that's it. I really recommend you to use MVVM. It makes your life much easier.
You may use Pages and frames to display pages.
Add the frame on tab like this:
<TabItem>
<Frame Name="Frame1"></Frame>
</TabItem>
Show page inside frame:
Frame1.Content = new MyPage1();

Categories