Xamarin SyncFusion, Cropped Image not passed properly? - c#

My goal is to have the cropped image that is edited in my CropPage.xaml (which holds the SfImageEditor to do this) passed to another class.
Here is the code to the CropPage.xaml file:
<SyncFusion:SfImageEditor x:Name="imgEditor"
ImageSaved="imgEditor_ImageSaved">
</SyncFusion:SfImageEditor>
Now I've edited the default toolbar at the top of the SfImageEditor page to have "Continue" as clickable, and all of this is in CropPage.xaml.cs. There's a lot more code of course but this is the main portion I believe matters:
// Assign function when a selection occurs.
imgEditor.ToolbarSettings.ToolbarItemSelected += ToolbarSettings_ToolbarItemSelected;
private void ToolbarSettings_ToolbarItemSelected(object sender, ToolbarItemSelectedEventArgs e)
{
// When the user is ready to move on, they press continue to move to the next page. Check specifically for that.
if(e.ToolbarItem.Text == "Continue")
{
// Get the image and transfer it to the next page here. (Is it really just the source?)
Navigation.PushAsync(new ProcessPage(imgEditor.Source));
}
}
Then of course, ProcessPage is just another class and its constructor RECEIVES the ImageSource. The important code is:
// CropPage will pass on the image source when the user is done cropping, and image will be used for the algorithm.
ImageSource m_savedImgSrc;
public ProcessPage(ImageSource imgSource)
{
// Get rid of the navigation bar.
NavigationPage.SetHasNavigationBar(this, false);
InitializeComponent();
// Place image on screen.
imgSpace.Source = imgSource;
}
The "imgSpace" from above is just a small area to show the would be cropped image and that code in the xaml file looks like:
<Image x:Name="imgSpace"
AbsoluteLayout.LayoutBounds="0.5, 0.3, 0.9, 0.65"
AbsoluteLayout.LayoutFlags="All">
</Image>
So the plan was simple, I crop an image with the SfImageEditor inside the CropPage, and pass it to ProcessPage to display. But no matter how I crop it, it goes to ProcessPage and there is the ORIGINAL image.
I guess I did assume that when I crop the image in CropPage that the new image displayed was in the source so passing source along as you seen would solve my problem. But unfortunately that wasn't true.
There's gotta be something I'm missing to explain why I'm not getting the cropped image I'd like. Any help is much appreciated!

Checked your requirement and shared code, and you can’t get the edited image from ImageEditor.Source property as it contains only the original ImageSource that was added in the image editor.
However, you can get the edited image stream from the image editor’s ImageSaving event as per in the below code snippet
<imageeditor:SfImageEditor Source="{Binding Image}" x:Name="editor" ImageSaving="imgEditor_ImageSaving"/>
Once done editing, click the Continue toolbar button and pass the edited image source.
private void ToolbarSettings_ToolbarItemSelected(object sender, ToolbarItemSelectedEventArgs e)
{
if (e.ToolbarItem.Text == "Continue")
{
// Call editor save method.
editor.Save();
}
}
private void imgEditor_ImageSaving(object sender, ImageSavingEventArgs args)
{
// Below line will stop the image saving to your machine. If you like to save the image in your machine, you can remove the below line.
args.Cancel = true;
//args.Stream contain edited image stream
var stream = args.Stream;
//Convert the edited stream to ImageSource
var source = ImageSource.FromStream(() => stream);
// Pass edited image source to the next page
Navigation.PushAsync(new ProcessPage(source));
}

Related

How to reset an image to default in Windows Form

I have created a picture box in my Windows Form. I set it to a particular image in the Properties section. I then coded a method which changes the image depending on criteria. How would i reset the image to initial default image i had after it has been changed?
Doing something as simple as:
pbMyImage.Image = pbMyImage.InitialImage;
This will reset it to the value you set in the InitialImage in the properties window.
Have you tried simply changing it to the default image again inside your code?
pictureBoxName.BackgroundImage = yourDefaultImage;
Aditionally, create a new void method and call for it whenever you want to reset it to default:
public static void setImageToDefault()
{
pictureBoxName.BackgroundImage = yourDefaultImage;
}
Then call for the method setImageToDefault();
We can reset picture box to its initial image easily. You can also check this property set in resource file for required picture box. For example if name of our picture box is "picFacial" then we can reset it like
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmePassportApplication));
this.PicFacial.Image = ((System.Drawing.Image)(resources.GetObject("PicFacial.Image")));
this.PicFacial.InitialImage = ((System.Drawing.Image)(resources.GetObject("PicFacial.InitialImage")));
This is an old question, but hopefully this will help others stumbling upon the same issue ... you can put your particular image in the ErrorImage in section of properties window NOT in the image section .... then your code will be
private void btnDelImage_Click(object sender, EventArgs e)
{
pboxImage.Image = pboxImage.ErrorImage;
}

Value always return null

First off, I'll explain my program.
I have one winform that I use as my control panel. From here I have the ability to take a screen shot of my desktop using the mouse to define the area. Much like how the snipping tool works within windows.
The screen shot is generated using my screen shot class which is ran by another form called Form1. This second form has no other code in it but is simply used to generate the rectangle the screen shot will use. From here, the taken screen shot is stored in the clip board and passed back to my Screenshot class.
Now, from here what I want is a picture box in my control panel to display this taken image. Exactly like how the snipping tool works. However, the code I have wrote to pass this image to the control panel from the screen shot class complains that the event handler is always returning as null.
The code I have wrote for this is as follows:
Image img = (Image)bitmap;
if (OnUpdateStatus == null) return;
ProgressEventArgs args = new ProgressEventArgs(img);
OnUpdateStatus(this, args);
I've tried commenting out the if statement but then the processing OnUpdateStatus throws an exception saying it no longer exists.
Now, in my control panel form I am trying to grab that image and display with the following code:
private ScreenCapture _screenCap;
public ControlPanel()
{
InitializeComponent();
_screenCap = new ScreenCapture();
_screenCap.OnUpdateStatus += _screen_CapOnUpdateStatus;
}
private void _screen_CapOnUpdateStatus(object sender, ProgressEventArgs e)
{
imagePreview.Image = e.CapturedImage;
}
I've spent hours on this but i cannot work out why the image won't display in the image box on my ControlPanel. Can anyone with a fresh set of eyes help me out? Why is it the screen shot image I take, isn't being displayed in my picture box held on my ControlPanel?
This is the problem, I think (there's a lot of code to skim through - a short but complete example would have been better):
ScreenCapture capture = new ScreenCapture();
capture.CaptureImage(...);
That's a new instance of ScreenCapture. It doesn't have any event handlers attached to it (which is why you're seeing a value of null for OnUpdateStatus. If you want to use the instance that you've created in the ControlPanel class, you'll want to pass that to your Form1 (e.g. in the constructor).

button.BackgroundImage = <pngfile> does nothing

I want to change the BackgroundImage of a button when you click once, and then change it back to the original when clicked again (and it will work over and over). My code snippet is this:
private void handButton_Click(object sender, EventArgs e)
{
if (handButton.BackgroundImage == WindowsFormsApplication1.Properties.Resources.Hands_Right)
{
handButton.BackgroundImage = WindowsFormsApplication1.Properties.Resources.Hands_Left;
}
else if (handButton.BackgroundImage == WindowsFormsApplication1.Properties.Resources.Hands_Left)
{
handButton.BackgroundImage = WindowsFormsApplication1.Properties.Resources.Hands_Right;
}
}
But when I run the program and click the button; nothing happens. The images are 32x32, and I can see the original image clearly. When clicked, the original image stays there. There are no other variables affecting this snippet (at least, a search for "handButton" only gets results from this snippet).
Any suggestions? I have no errors, so I suspect that I'm going about this wrong. Is there a better way to change images back and forth?
The Properties.Resources class doesn't work the way you think. A property like Hands_Right actually returns a new bitmap, not whatever object was returned previously. That wouldn't work very well since modifying the bitmap would also modify the property from its design.
So your if() statement expressions never evaluate to true. Keep track of the button state separately.

I want to load default image in picturebox if the user does not select an image in C#

I am developing a window form in C#. In my window form there is a picture box. I want if the user does not select an image then the default image will be load in picture box which is save in my project folder.
thanks in advance
I guess you want to know how you can get the picture from the project folder right?
First add the picture to you project (add existing item) and set the Build Action to Embedded Resource:
then the following code do the trick:
private void SetPicture()
{
var assembly = System.Reflection.Assembly.GetExecutingAssembly();
using (var imgStream = assembly.GetManifestResourceStream("DataGrid.TestImage.jpg"))
{
var img = new Bitmap(imgStream);
Picturebox.Image = img;
}
}
where "DataGrid" is MY project-name (you have to insert your own) and "TestImage.jpg" is the name of your Image (if you put it into a folder you might have to give the foldername too).
Picturebox is a PictureBox-Control I set the image to.
Either set an image in the designer and override it in code
for instance you can place this after user selection:
if (someConditionIndicatingUserAction)
pictureBoxControlName.Image = UserSelectedImage;
Or, you can store a default value in a variable and set the picturebox image to it (if the user selects nothing, you don't change the value, and if he does, change it accordingly)

WP7 How do I change the image of a button when clicked

Right now I have a grid of six buttons, all with different images inside them. I have another set of six images, that are the original just with a gray tint to represent you selecting it. How do I change the image to the button to the new "selected image" when I select the button.
I am assuming you do it in the method:
private void button1_click(object sender, RoutedEventArgs e)
{
}
I'm having trouble figuring out what to put inside here. Normally I would think it would be something like:
button1.image = "image path";
However when making a WP7 application you cannot use the image keyword. Any advice on how to change the image of a button when clicked?
Write where you want to change the image
var brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri("/Images/YourImage.png",
UriKind.Relative));
btn.Background = brush;
In Silverlight (upon which the Windows Phone 7 framework is built) the Button control does not have an Image property. I presume that you have created your original buttons by placing an Image element as the child of the Button. On the assumption that you want the same behavior for a whole set of buttons, then it might make sense to use visual states instead. You coudl achieve a consistent look-and-feel by changing the opacity of the ContentPresenter, e.g. An Opacity of 0.75 for the "Normal" state and and Opacity of 1.0 for the "Selected" state.
Determing which button is the selected one would be the trickier part, but if you wrap your buttons in a ListBox then you can use the "Selected" visual state in the ItemContainerStyle.
If you want to continue with the approach that you've already taken, then given that you know that the content of the button is an Image, you could do something like the following:
private void button1_click(object sender, RoutedEventArgs e)
{
Button source = (Button)sender;
Image content = source.Content as Image;
if (null != content)
{
content.Source = new BitmapImage(new Uri("image path"));
}
}
In this approach you would, of course, also need to handle reverting the other buttons back to their "Normal" state, which the ListBox approach would handle for you.
What you're doing is a really good learning exercise - you'll learn lots about Silverlight by experimenting like this.
In addition to manually adjusting the image to match the button press state, I believe you can also achieve the effect you are looking for - that the button image becomes "gray" when pressed - you can do this using "Styles" and using "Behaviors". Take a look at posts like:
http://mstechno.wordpress.com/2009/08/28/silverlight-3-how-to-customize-a-button-with-expression-blend-3/
Windows Phone 7 (WP7) Change a button's background color on click
Some of the XAML in this might look daunting - and using Expression Blend takes some time to get used to - but you will get there. Good luck!

Categories

Resources