c# how to clone picture box 1 into pb 2? - c#

I use two functions to load live camera and picture into picture box 1 and I want the same to be displayed in picture box2 also...
whatever comes in pb1 should also come in pb2.

Unfortunately, PictureBox does not have event like OnImageChanged, so you got to work around with it.
One way to work around with it is by creating your own MyPictureBox class derived from PictureBox (winform) in which it has its own (overshadowing) Image property. Then in the class you declare ImageChanged event and well as its handler. Then, in the setter of the Image property, you could call ImageChanged event.
When the ImageChanged occurs, you can change the other PictureBox image too.
Alternatively, you may want to make use of the existing (similar) LoadCompleted event of the PictureBox and then triggers the other PictureBox to get the new image.
See if any method may work for you.

Hi #user3004860 in my sample , I have quietly insert picture to the file folder when I put a picture in the box , so that you can use it in anywhere ,hopefully my idea can help you to deal with your question , as following code is the sample
if (!string.IsNullOrEmpty(fileExt))
{
var strExt = fileExt.ToLower();
var newFileName = string.Format("{0}{1}{2}", DateTime.Now.ToString("yyyyMMddHHmmss"), Random.Next(0, 9999).ToString("D4"), strExt);
var newOFileName = "o" + newFileName;
string imgUrl = "/Files";
var directory = new System.IO.DirectoryInfo(Server.MapPath("~"+imgUrl));
if (!directory.Exists)
directory.Create();
try
{
upload.SaveAs(directory + "/" + newOFileName);
string res;
res =
string.Format(
"top.$('.mce-btn.mce-open').parent().find('.mce-textbox').val('{0}{1}').closest('.mce-window').find('.mce-primary').click();",
imgUrl + '/', newOFileName);
return Content(res);
}
catch
{
return Content("error");
}
}

Related

How to check if a picture box contains a certain image using an IF statement

I am trying to check if a PictureBox contains a certain image, the way I am trying to do it seems like it would work in my head, however, does not, i am not sure if there is any other way to check if the picturebox on the form contains a certain image.
private void user_btn_Click(object sender, EventArgs e)
{
//If statement to check if the forms picture box contains a certain image
if (pictureBox1.Image == Resources.user_male_white_red_brown)
{
this.Hide();
UserProfile User = new UserProfile();
User.ShowDialog();
User.pictureBox1.Image = Resources.user_male_white_red_brown;
this.Close();
}
else if (pictureBox1.Image == Resources.user_female_olive_orange)
{
this.Hide();
UserProfile User = new UserProfile();
User.ShowDialog();
User.pictureBox1.Image = Resources.user_female_olive_orange;
this.Close();
}
}
Although PictureBox Image might be identical as the one in the resources but they are not the same as reference. (Imagine like you have 2 copy of the same photo although their image is the same but they are 2 separate photos).
There are a few ways you can do it, one of them (a simple one) is to set the picturebox Tag to a relevent value whenever setting its image and compare that value instead of comparing images:
User.pictureBox1.Image = Resources.user_male_white_red_brown;
User.pictureBox1.Tag = "user_male_white_red_brown";
And then:
if((string)User.pictureBox1.Tag == "user_male_white_red_brown")
{
// your logic
}
This way you need to set PictureBox tag whenever you set its image.
Another way would be loading all images from resources to an array and setting the PictureBox image from the array, this way as the reference of both images (picturebox.Image and the image item in the array) are the same comparison would work for images but I think the first solution is easier.

how to cast (or convert) an ImageSource to FileImageSource [duplicate]

I am trying add a background image using the image property in button. The issue I'm facing is that i can't set StreamImageSource as button background. I encountered the error given below if I try to do so.
The Code I use to set Image:
ImageSource iconsource =ImageSource.FromStream(() => new MemoryStream(ImgASBytes));
Button Icon = new Button ();
Icon.Image = iconsource ;
The Error I encounter:
Error CS0266: Cannot implicitly convert type 'Xamarin.Forms.ImageSource' to 'Xamarin.Forms.FileImageSource'. An explicit conversion exists (are you missing a cast?)
ImageSource.FromStream () returns a StreamImageSource (see docs). Button.Image accepts only FileImageSource (see docs).
It means that what you're trying to achieve won't work, no matter how hard you try to cast one into the other.
Button.Image will accept images stored as resources in your platform projects, and loaded either with:
Icon.Image = ImageSource.FromFile ("foobar.png");
or
Icon.Image = "foobar.png";
The accepted answer is true that you can't cast StreamImageSource to FileImageSource, I think that the real question is about how to share images in a PCL and use them on a button, just like one would when creating an Image forms control.
The answer is to have a Grid which contains both a Button and an Image object, where the Image overlaps the Button.
For example, the C# code might look like this:
ImageSource imageSource = ImageSource.FromStream(() => new MemoryStream(imageAsBytes));
Button iconButton = new Button ();
iconButton.VerticalOptions = LayoutOptions.FillAndExpand;
iconButton.HorizontalOptions = LayoutOptions.FillAndExpand;
var image = new Image();
image.Source = imageSource;
// So it doesn't eat up clicks that should go to the button:
image.InputTransparent = true;
// Give it a margin so it doesn't extend to the edge of the grid
image.Margin = new Thickness(10);
var grid = new Grid();
// If we don't set a width request, it may stretch horizontally in a stack
grid.WidthRequest = 48;
// Add the button first, so it is under the image...
grid.Children.Add(iconButton);
// ...then add the image
grid.Children.Add(image);
You may have to play with the sizes and thickness values but this should get you a clickable button with an icon.
As of Xamarin.Forms 3.4.0 you can now use ImageButton. You can use embedded images by using an extension method explained in this MS document
Careful with upper- and lowercase in filenames.
I was wondering, why my button-images were shown properly on the simulator, but not on my iPhone.
On the device the filename must match exactly, the simulator doesn't care about upper- and lowercase in filenames.
I use this and it works
var imageA = new Image();
imageA.Source=(FileImageSource)ImageSource.FromFile(allergeneLocation)};
or
var imageA = new Image()
{
BackgroundColor = Color.Teal,
Source = (FileImageSource)ImageSource.FromFile(allergeneLocation)},
};
Here is what I tried:
Button refreshBut = new Button
{
Image = (FileImageSource)
(ImageSource.FromFile("refreshBut.png"))
};
While it compiles I then get an unhandled null reference exception with the description: Object reference not set to an instance of an object. I am not sure if this will help anyone else try to solve this but I am at the same wall.

Using image transparency between Picture Boxes stores as Arrays

I am using c# and winForms to create an application that allows me to have 6 Picture Boxes (each with a different image) on top of one another. Each image has alot of white space so i am trying to use transparency to allow the user to toggle images on and off. When an image is toggled off, the parent/child of all the other images will be updated to allow the transparency to work.
I First created a custom class:
public class MyImageWrapper
{
public MyImageWrapper Parent { get; set; }
public PictureBox PictureBox { get; set; }
public MyImageWrapper(PictureBox i, MyImageWrapper parent = null)
{
Parent = parent;
PictureBox = i;
}
}
Then I Created the Array:
MyImageWrapper[] pictureBoxArray = new MyImageWrapper[6];
This next code is a test to Check if transparency works:
pictureBox[0] = new MyImageWrapper(pictureBox1);
pictureBox[1] = new MyImageWrapper(pictureBox2);
pictureBox1.Image = (Image.FromFile(""+ Application.StartupPath +"../../../images/imageA.png"));
pictureBox2.Image = (Image.FromFile(""+ Application.StartupPath +"../../../images/imageB.png"));
pictureBoxArray[1].Parent = pictureBoxArray[0];
Unfortunately I do not receive any error messages so it seems to be working. However when I run the program Picture Box 2 is not a child of picture box 1 (and thus transparency does not work)
I have not gotten as far as setting the images to be turned on or off yet, I am still trying to sort out the transparency.
The problem specifically is this line:
pictureBoxArray[1].Parent = pictureBoxArray[0];
When testing I replaced the above line with:
pictureBox2.Parent = pictureBox1;
This does exactly the same thing (but works!). However the way my program needs to work later I cannot do it like this. I need to fix the Array version.
Any help would be greatly appreciated.
Wouldn't you need to put:
pictureBoxArray[1].PictureBox.Parent = pictureBoxArray[0].PictureBox
?
The two lines:
pictureBoxArray[1].Parent = pictureBoxArray[0];
pictureBox2.Parent = pictureBox1;
You have put are not equivalent. In one you are assigning the parent of the ImageWrapper and in the other you are assigning the parent of the PictureBox.
I don't know if this is an error in how you have presented the code or in your program itself.

Image name as string, use it to make image visible WPF with C#

I have an image name as a string. The real imagename on the form is called "image". So i get something like this:
image.Visibility = Visibility.Hidden;
string imageName = "image";
// need something here to make it usable...
changedImageName.Visibility = Visibility.Visible;
Now, a string can not be used in combination with the Visibility property.
I cant really find what i must make the string to, to make it usable for the visibility property.
If i see this page: http://msdn.microsoft.com/en-us/library/system.windows.visibility.aspx
Do I understand correct that I make it a "enum" ? And if yes, how do I get a string to that property?
EDIT 1
I see I have not been explaining it proper enough.
I forgot to mention I am using a WPF form.
on this form, I have put an image.
In the initialize part, the image get set to hidden.
so for example the imagename I named "Image"
so I use image.Visibility = Visibility.Hidden;
later on in my code, I want to make the image visible again, depending on what the user does.
so, instead if just using the name to get the image visible again, I want to use a string.
this string is looking exactly as the name of the image.
but i cant use the string in combination with the Visibility function.
but i cant find anywhere what i must make this string to, to be able to use that visibility option on it.
hope i explained a bit better now :).
Later on, i will have multiple images on the WPF window.
So the key is that i will use the string, that is corresponding with the name of the image.
Depending on what the user has input into the string, some image will or will not show.
EDIT 2
If you have:
String theName = ImageName.name
you can get the name of the image into a string, so you can do stuff with it.
i am looking for a way to do the exact opposite of this. So i want to go from a string, to that name, so after that i can use this to control the image again.
Edit 3
some example:
private void theButton_Click(object sender, RoutedEventArgs e)
{
//get the Name property of the button
Button b = sender as Button;
string s = b.Name;
MessageBox.Show("this is the name of the clicked button: " + s);
//the name of the image to unhide, is the exact same as the button, only with IM in front so:
string IM = "IM";
IM += s;
MessageBox.Show("this string, is now indentical to the name of the image i want to unhide, so this string now looks like: " + IM );
// now, this wont work, because i cant use a string for this, although the string value looks exactly like the image .name property
// so string IM = IMtheButton
// the image to unhide is named: IMtheButton.name
IM.Visibility = Visibility.Visible;
}
looks like you're using WPF, so you can create a boolean to visibility converter and use it with a boolean (and create a method that receives string if necessary) and just use:
<ContentControl Visibility="{Binding Path=IsControlVisible, Converter={StaticResource BooleanToVisibilityConverter}}"></ContentControl>
or any other converter...
check this links:
http://bembengarifin.wordpress.com/2009/08/12/setting-visibility-of-wpf-control-through-binding/
http://andu-goes-west.blogspot.com/2009/05/wpf-boolean-to-visibility-converter.html
http://msdn.microsoft.com/en-us/library/system.windows.controls.booleantovisibilityconverter.aspx
EDIT 1:
so then you will have to iterate over the images and check if your string is equals to name of the Image class.
something like this (not tested):
foreach (Image img in container.Items)
{
if img.Name == yourMagicallyString;
{
img.Visibility = Visibility.Visible;
}
else
{
img.Visibility = Visibility.Hidden;
}
}
If I understand correctly, you are trying to find a control based on the name or ID of the control. If so, try this:
Control changedImage = this.Controls.Find("image", false)[0];
Depending on what you are targeting and what version you might need to tweak a little
EDIT Updated per #Alexander Galkin comments about Find returning an array. There should definitely be some checking and whatnot but I'm leaving that up to the OP.
EDIT 2 For finding a control by name in WPF see this post.
The code I was looking for:
object item = FindName(IM);
Image test1 = (Image)item;
test1.Visibility = Visibility.Visible;

How to get the name of a control under the mouse pointer ( without making an event handler for each control )?

I have an mdichild, and an eventhandler for draganddrop so when I drop an image file in my form, a picturebox ( name = dpic ) is created with that image.
I have another eventhandler which is for dpic_Click, so when I click on the image, my form's text is the name of that image.
After the first time I drop an image, another dpic is created, because you can't have two cotrols with the same name. C# automatically changes the name and that makes my event handlers only work for the last image I dropped in. I think if I could make an event handler for my mdichild that gets the name of the control that is under the mouse pointer I could simply change back the image I am pointing at.
UPDATE
Here is my code , I made an event handler for droping in my mdichild :
void mdiChild_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
dpic = new PictureBox() ;
string[] filepath = (string[])e.Data.GetData(DataFormats.FileDrop);
Image image = Image.FromFile(filepath[0]);
dpic.Image = image;
dpic.Tag = Path.GetFileName(filepath[0]);
this.ActiveMdiChild.Controls.Add(dpic);
dpic.ContextMenuStrip = this.contextMenuStrip1;
this.ActiveMdiChild.Refresh();
dpic.BringToFront();
this.ActiveMdiChild.ActiveControl = dpic;
dpic.Click += new EventHandler(dpic_Click);
// _____this helped me do it_________________________________________________
foreach (Control c in this.ActiveMdiChild.Controls)
{
c.Click += new EventHandler(c_Click);
}
// ________________________________________________________________________
}
}
What you are looking for is a sender. The sender will tell which image was clicked and will allow you to get its name.
PictureBox picSender = (PictureBox)sender;
label1.Text = picSender.Name;
EDIT : You put that in the pic_Click event
I don't understand exactly what you want to do here, possibly layering new pictureboxes with dropped images on the form?
If that is so you can use a
List<PictureBox> picboxes = new List<PictureBox>();
and where you do:
dpic = new PictureBox() ;
change to
PictureBox dpic = new PictureBox() ;
picboxes.Add(dpic);
this.Controls.Add(dpic);
But please note that you cannot have unlimited controls declared.

Categories

Resources