I got this issue with displaying a image that i allready have added as a resource. I am guessing i am missing something vital but i cant find what it is. I am hoping that someone has a better idea of what i am doing wrong atm.
I have added an .bmp image into the solutiontree and changed the build action properties of that image to Embedded resource but i cant figure out how to call that image from the pipe?
as the user clicks a button the image should be sent to the imagebox, the code i have written so far looks like this:
this is ofs only the button_click code:
private void button1_Click(object sender, EventArgs e)
{
//Show image in the picturebox of selected cake
Image image = Image.FromFile(fruitcake.jpg);
pictbox.Image = image;
pictbox.Height = 163;
pictbox.Width = 223;
choice = 1;
lblCookiesPerGram.Text = string.Empty;
}
Anyone has an idea of what i am doing wrong or can i do this in another war? mind thou its 4 buttons the user clicks and there is a image for each one ;)
//Regards
If you go to your Solution - Properties window and select the Resources tab and add the image through this manager, then the images can be directly referenced like this:
Image image = Properties.Resources.fruitcake;
To retrieve an image from a resource, you can do something like:
using (Stream imgStream = Assembly.GetExecutingAssembly()
.GetManifestResourceStream(
"MyNamespace.resources.fruitcake.jpg"))
{
var image = new Bitmap(imgStream);
pictBox.Image = image;
pictBox.Height = image.Height;
pictBox.Width = image.Width;
}
Related
I'm trying to do something when I click image displayed inside pictureBox1.
pictureBox is loaded with this code:
string imgpath = #"img\256.png";
pictureBox48.Image = Image.FromFile(imgpath);
Then control is released to me so I can see that the picture loaded correctly.
Then i click the picture:
public void pictureBox48_Click(object sender, EventArgs e)
{
string variable1 = pictureBox48.ImageLocation;
Form3 fo = new Form3(variable1);
fo.ShowDialog();
}
This doesn't work. When I debug the code I see that variable1 stay null, that is pictureBox48.ImageLocation is null. Why is that? Shouldn't it be the path to the image that is assigned there?
You can't get the image path when you set the image using the Image property because you are assigning an Image object which can come from different sources.
Set the image using ImageLocation.
string imgpath = #"img\256.png";
pictureBox48.ImageLocation = imgpath;
When you click in the PictureBox you can get the path using the same property:
public void pictureBox48_Click(object sender, EventArgs e)
{
string variable1 = pictureBox48.ImageLocation;
Form3 fo = new Form3(variable1);
fo.ShowDialog();
}
When dealing with Image or PictureBox I would recommend to not use something like Location or Path of the image. Assume that when the image is loaded user removes it from the hard drive and you're left with the code full of errors.
That's why you should rely on Image itself as it contains every information about the image like pixel format, width, height and raw pixel data.
I would recommend you to just copy the image, not the path to the file.
This piece of code should give you a hint:
pixtureBox48.Image = Image.FromFile(imgPath);
// above code assumes that the image is still on hard drive and is accessible,
// now let's assume user deletes that file. You have the data but not on the physical location.
Image copyImage = (Image)pictureBox48.Image.Clone();
Form3 fo = new Form(copyImage); // change .ctor definition to Form(Image copy)
fo.ShowDialog();
Im a total C#/programming beginner.
I try to display alot of pictures like an album or grid like.
It looks like this so far:
http://i.imgur.com/LC7lhXi.jpg
But there are empty places and when I scroll down it looks even worse: http://i.imgur.com/QuFOgr1.png
I created a UniformGrid inside a ScrollViewer. That was the easiest way for me to get what I want. Maybe not the best solution. If I add more pictures the images are getting smaller. I think thats cause of the uniform grid.
Here are two code example how I did that.
js.movie1 is an object which contains a few members. js.movie1[0].poster is the file name of the first picture (there are 275 pictures).
This object is created by the JSON.NET framework. It just reads a JSON file set some variables and I put the object in a list.
for example js.movie1[0].poster is the file name of the poster as a picture, js.movie1[0].title is the title of the movie and so on.
private void btnShow_Click(object sender, RoutedEventArgs e)
{
uGrid.Children.Clear();
string strWebAdress = #"https://image.tmdb.org/t/p/original/";
for (int i = 0; i < js.movie1.Count; i++)
{
Image img = new Image();
BitmapImage bimg = new BitmapImage(new Uri(strWebAdress + js.movie1[i].poster));
//bimg.sou
img.BeginInit();
img.Source = bimg;
img.EndInit();
uGrid.Children.Add(img);
}
Or I did this:
private void btnShow_Click(object sender, RoutedEventArgs e)
{
uGrid.Children.Clear();
string strWebAdress = #"https://image.tmdb.org/t/p/original/";
List<string> listImage = Directory.GetFiles(strPath, "*jpg").Select(System.IO.Path.GetFileName).ToList();
List<Image> imgList = new List<Image>();
for (int i = 0; i < js.movie1.Count; i++)
{
Image Controlimage = new Image
{
Source = new BitmapImage(new Uri(strCoverUrl + js.movie1[i].poster)),
};
uGrid.Children.Add(Controlimage);
}
My problem is that there are empty spaces between some pictures, I cant resize them like I want. And I want to scroll while the program is loading the pictures.
The program needs 1,5 GB of Ram and alot of CPU power so I need a betetr solution for this.
With Foreach I can scroll but I created a array for that. I dont know how to count in foreach with a object. Atleast not in the way my object is used.
There have to be a way to load and display pictures from an URL which doesnt block the UI and needs so much power.
Like I said Im a bloody beginner ;)
Greetings Fabian :)
I inserted an image into a button, but it's not a background image. So, I want to resize the image's size that I want for example Height=30, Width=20 or sometime Height=50, Width=50. Some people told me that it's impossible to resize an image in a button if I inserted it as an background image it's possible.However, if I insist to resize the image it's possible? I don't believe that nobody can do it.
Is this a windows form application? Forgive me if I didn't understand the question, but you should be able to resize the image from the Properties menu if you're using a PictureBox. Right-click on the image you inserted, select properties and there should be a field for Size.
I am assuming that you want to resize the image present inside the button. You can try something like this in Designer File:
//
// ButtonName
//
this.ButtonName.AutoSize = false;
Image image = new Bitmap(customWidth,customHeight);
using (Graphics g = Graphics.FromImage(image )) {
g.DrawImage(FileLocation, 0, 0, customWidth, customHeight);
}
this.ButtonName.Image = image;
This will help in button resizing but the picture won't be clear if much increased.
Button.Resize += Button_Resize;
private void Button_Resize(object sender, EventArgs e)
{
Button button = (Button)sender;
Image resize = new Bitmap(button.Image,new Size(button.Image.Width, (button.Height - 13))); button.Image = resize;
}
I am trying to click on an image in a datagridview and then write its image/file name into a textbox so I can access this from elsewhere.
First I try just a small app to make sure I can make it all work. A Dialog contains the dataviewgrid and I put a bitmap into it as below:
public ChooseFormat()
{
InitializeComponent();
dataGridView1[0,0].Value = new Bitmap(#"C:\a\eggs\grid_app\grid_app\bin\Debug\graphics\1L5HQ60.bmp");
}
Now I click on the image but all the things I have tried I cannot get hold of the file name. The closest I get is below but this returns "System.Drawing.Bitmap" and not the file name. I am sure this must just be a tweak here to make it work but I have tried teh few things I know and nothing is working.
void DataGridView1CellContentClick(object sender, DataGridViewCellEventArgs e)
{
txtbx_choice.Text = dataGridView1[0,0].Value.ToString();
}
Drilling into the cells's data in the debugger doesn't bring up any info on the source of it. Maybe I have overlooked something..
One simple solution is to store the filename in the cell's Tag property:
string fileName = #"C:\a\eggs\grid_app\grid_app\bin\Debug\graphics\1L5HQ60.bmp";
dataGridView1[0,0].Value = new Bitmap(fileName );
dataGridView1[0,0].Tag = fileName ;
Now you can always access it:
string displayedFile = dataGridView1[0, someRow].Tag.ToString();
I have placed a Picture Box on the same form and this how I am displaying the ImageColumn's data (an Image ) in picture box
pictureBox1.Image = (Image)dataGridView1[0, 0].Value;
I want to change the background image of a panel in a C# Windows Forms application. The image I want to set as the background is located in a resource folder in Solution Explorer. How do I use it in code?
I tried this:
panel1.BackgroundImage = Properties.Resources.Chalkboard;
But it didn't work.
I tried the same code like you did, and it works fine when I hit a button.
private void pnlBgBtn_Click(object sender, EventArgs e)
{
panel1.BackgroundImage = Properties.Resources.image;
}
The name 'image' in 'Properties.Resources.image' should be the name you gave to the image.
The right name of the image should be the name shown in your project properties under project-proje.
The properties.Resources class doesn't return every resourse as an image so you have to apply a cast to Image like this
panel1.BackgroundImage = (Image)(Properties.Resourses.Chalkboard);
You can try this out:
Bitmap bmp = new Bitmap(System.Reflection.Assembly.GetEntryAssembly().
GetManifestResourceStream("MyProject.Resources.myimage.png"));
panel1.BackgroundImage = bmp;
If you want to set the background image of a panel on page load then you have to write this code:
private void panel1_Paint(object sender, PaintEventArgs e)
{
Assembly asm = Assembly.GetExecutingAssembly();
Bitmap backgroundImage = new Bitmap(asm.GetManifestResourceStream("Image913.jpg"));
e.Graphics.DrawImage(
backgroundImage,
this.ClientRectangle,
new Rectangle(0, 0, backgroundImage.Width, backgroundImage.Height),
GraphicsUnit.Pixel);
}
If you want set the image except the panel, load use this code:
Bitmap bmp = new Bitmap(System.Reflection.Assembly.GetEntryAssembly().
GetManifestResourceStream("MyProject.Resources.photo0018.jpg.png"));
panel1.BackgroundImage = bmp;
You can create an icon resource in Properties project folder. When you opened Properties click on Resources.resx and there Add Resource->Add New Icon menu items. This will create an icon. You can also load an icon from an existing file into the resource, in this case the icon will be built in your executable. So, when your icon added as a resource it will be given some name.