How to reset an image to default in Windows Form - c#

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;
}

Related

Xamarin SyncFusion, Cropped Image not passed properly?

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));
}

Updated icon image won't show in document tab

I just found DockPanel Suite and am learning to use it.
I downloaded v2.9 and am using it with C# in VS2013. I created a simple Windows Forms MDI app. It has one type of child form which is a simple form with only a rich text box control in it. I loaded a few of these as documents. I added an icon to the document's tab to indicate whether the rich text box text has been saved or not.
I change the tab icon anytime the user types in the rich text box through the use of the text_Changed event:
private void txtCode_TextChanged(object sender, EventArgs e)
{
//The text has changed, set the warning icon.
this.Icon = MyApp.Properties.Resources.Warning;
}
The problem I have is that the icon does not update when running at full speed. It updates fine when I am single stepping through the above event. It also updates fine when I load a file within the form's Load event;
private void frmCode_Load(object sender, EventArgs e)
{
//Get the full file path.
string sFile = Path.Combine(cCoCoIDE.CodeBaseRootFolder, Path.GetFileNameWithoutExtension(cCoCoIDE.CodeBaseFile), this.Text);
//Load the source file into the code window.
//A few validations.
//Make sure the file exists.
if (File.Exists(sFile))
{
//File is there. Is it empty? Load only if not empty.
if (new FileInfo(sFile).Length > 0)
{
txtCode.LoadFile(sFile);
//I had to add the following because the text changed
//event fires when I load the file and I need to start
//off with the green checkmark icon.
this.Icon = CoCoIDE.Properties.Resources.GreenChk;
}
}
}
I tried the following after I change the icon (in the Text Changed event):
this.ShowIcon = true;
this.Refresh();
this.Update();
Note: I tried these one at a time. The above is just a list of methods.
Can anyone help me with this? Thank you! SgarciaV
I found the solution. See here:
Dockpanel Suite: Image not updating

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).

Create image from file programmatically asp.net C#

I am writing project in asp.net C#. I want to create Image programmatically by the following code:
protected void Page_Load(object sender, EventArgs e)
{
Image image = new Image();
image.ID = "image11";
image.ImageUrl = "a.jpg";
image.ImageAlign = ImageAlign.AbsMiddle;
image.Visible = true;
}
But nothing is displayed when I run the project.
How to create image from file and display it in the page by writing code in .cs file?
You have created an image control but you have not added it to your form. Write the below code to add the image control to your form.
form1.Controls.Add(image);
At this point, you have just created an image, but you haven't added it to a control or page context to be displayed. You essentially said
int x = 10;
but then never did anything with x.
ASP.NET uses composition, so it maintains a collection of controls, with each control also containing a collection of children nodes. You need to add the image to a container. For instance, if you want to add the image to a panel named myPanel, it would be
myPanel.Controls.Add(image);
Check out this article.
You will need to create panel and then you will have to add that image to panel.
Panel Panel1= new Panel();
form1.Controls.Add(image);

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)

Categories

Resources