Changing Image to another Image in Resources folder (VisualStudio 2015 / c#) - c#

I'm trying to change the Background Pic of a Button to another Pic that I saved in the Resources Folder. Currently The Code is the following (FinalDStagePickBANNED.png being the Pic I want it to change to):
private void FinalD_Click(object sender, EventArgs e)
{
BanFDButton.Image = (Image)Properties.Resources.ResourceManager.GetObject(FinalDStagePickBANNED.png);
}
The Thing is, that it apparently finds no Pics in the Resources-Folder, although there are like 10 pics.
Solved: The solution to the problem was to put the FinalDStagePickBANNED into quatation-signs, and to remove hte .png ending.

Related

Open image from PictureBox in windows photo viewer using ImageLocation

I am trying to open an image from the PictureBox using ImageLocaton Property, because I want in the future to store the images on an online Database.
I've tried this code but says that pictureBox1.ImageLocation.ToString() is null:
private void pictureBox1_Click(object sender, EventArgs e)
{
Process.Start(pictureBox1.ImageLocation.ToString());
}
I also have seen it in the properties tab, but why?
You just need to set the ImageLocation of the pictureBox to set its Image (not get it that is obviously null if not set):
pictureBox1.ImageLocation = someurl;

trying to change a picture box c#

Im trying to make my picture change when a button is clicked so that the player can see the character attack. When they do so, so the picture should change to the attack image when the button is clicked and then back to the old image when the opponents turn starts.
All the images are saved in a folder named "images" inside the project folder, which is inside the "WindowsFormsApplication1" folder, but it says it cant find namespace images inside windowsformsapplication1
Here is the code I am using to change the image:
private void ArBut_Click(object sender, EventArgs e)
{
if (playerturn == true)
{
Ar.Image = global::WindowsFormsApplication1.images.archeratack.jpeg;
drhp = drhp - 15;
DrHP.Text = drhp.ToString();
checkend();
playerturn = false;
dratak();
}
}
You can't just set the image like that. First of all, because thats not a String, it thinks you are trying to access a code element. Obviously images is not a namespace or class within your code, so you get the compiler error.
You need to use PictureBox.Load (MSDN)
Ar.Load(#"./images/archeratack.jpeg");
You could try to create an Image object and use Image.FromFile("url")

How to Show Other Forms Using DevExpress tileItem in tileControl?

I am new in C# and DevExpress. I'm trying to show another form by clicking a tile in the tileControl group but it doesn't show up. I just right-clicked at the tileControl, clicked view code and manually declared this since this doesn't automatically shows up if you double click a tile.
private void addTile_Click(object sender, EventArgs e)
{
var xForm2 = new XtraForm2();
xForm2.Show();
}
I just right-clicked at the tileControl, clicked view code and manually declared this since this doesn't automatically shows up if you double click a tile.
If I understand this correctly, you did not build your project using the C# code listed above, but only edited the source.
That does not work because what you edit in this way is not being loaded into the app.
Of course, I could be misunderstanding what you wrote.

How to change the background of a button programatically?

I need to change the BackgroundImage of a button on click of another button (In Windows Forms in C#). But I can't find out how to do it!!
I searched on the internet and found many examples and all of them use ImageBrush, ImageSource etc.... but these don't work on my application, it shows me errors every time I Use them.
I read on the internet that I have to add this namespace:
using Windows.UI.Xaml.Media.Imaging;
But it shows me an error on the begging which says to add this System before Windwons and when I add it:
using System.Windows.UI.Xaml.Media.Imaging;
it shows me than the error at UI .... I can't figure it out how to solve this!!
Please help me guys!
To Change Background Image of a button there are two ways i know.
Add the Image to the resources folder of your project and use.
private void button2_Click(object sender, EventArgs e)
{
button1.BackgroundImage = Properties.Resources.ImageName;
}
Use Image.FromFile();
private void button2_Click(object sender, EventArgs e)
{
button1.BackgroundImage = Image.FromFile(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) + "//Card1.png");
}
You are trying to use solutions for WPF in Winforms. This will not work.
The class you need is System.Drawing.Image (or System.Drawing.Bitmap, which inherits from Image).
Bitmap b = new Bitmap(#"C:\myBitmap.jpg");
myButton.Image = b;
Be sure to call Dispose on your Bitmap if and when it is no longer in use.

No Image display in c#

I created a simple windows form application C#, that is supposed to show a picture. I am following a tutorial from here Following is the code of form1.cs
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
// Construct an image object from a file in the local directory.
// ... This file must exist in the solution.
Image image = Image.FromFile("Picture1.png");
// Set the PictureBox image property to this image.
// ... Then, adjust its height and width properties.
pictureBox1.Image = image;
pictureBox1.Height = image.Height;
pictureBox1.Width = image.Width;
}
}
The image is present in the solution folder, but still nothing is displayed in the window when the application is run. There is no compilation error.
Update
Its solved now.
Make sure the PictureBox click event handler is registrated. It is not sufficient to copy the example code. (I'm just guessing)
Without a full path, the image won't load from the solution directory.. it will load from the directory where your executable is executed from.. which is either the Debug or Release folders when you're running from Visual Studio.. depending on what Profile you're running it with.
So put the image in the /bin/Debug folder and run your program again.
A better option would be to add this image to the project and do either of the two:
Add as a resource. It can later be used with strong typing.
Set this option on the file: Copy to Output Directory = Copy if newer.
It is not a good practice to add anything manually under /bin/, because those folders are volatile and can be erased by Visual Studio at any moment (usually on the next rebuild).

Categories