Im making a Random Generator with Windows Forms with Images and I use the random pick with resources pictureBox1.Image = Properties.Resources.heart;
Now, the "heart" should get removed from the List, to prevent getting "heart" again.
Here I thought, that I just use int firstCard = randomCard.Next(cards.Count); and I want to use the int firstCard as Properties.Resources.cards[firstCard], because behind Properties.Resources. comes the resource name. But the string doesnt work there, and I dont know how to fix that. Pls help.
Thank you
Pults
Add all your images to a List<Image> - if you have 100 images your list will end up with 100 things
Pick one at random and also remove it. Make the upper bound of random the number of things in the list
//do this once outside the loop that adds images: var r = new Random();
Then the loop that adds images
var x = r.Next(imageList.Count);
var i = inageList[x]; //get the image
imageList.RemoveAt(x); //can't get it again
Note that setting the image of a picture box cannot be done in addition; you'll need multiple picture boxes
Side note, you might find it easier to keep your images in an ImageList (easier to index numerically) - the documentation for it also has some useful/helpful example codes that iteratively draws images into a PictureBox
Also, someone else (maybe doing the same exercise as you :) ) wondered how to get images by string name..
Oh yeah, someone asked the same, didnt find it.
Yea the "ResourceManager.GetObject" is exactly what I needed.
Thanks for the quick response and instant solve!
Related
I have 4 picturebox. The names: pb1, pb2, pb3, pb4
And I have 4 resource file: cards_club, cards_diamon, cards_heart, cards_spades
The resource files contains some french card picture. One of the names is: Cards-6-Club.svg
So my problem is: how to reflect them using a random number.
I mean - here is the main part of the code:
Random rnd = new Random();
int color = rnd.Next(1,4+1);
int value = rnd.Next(1,13+1);
int pb_num = rnd.Next(1,4+1);
textBox1.Text=color.ToString()+" "+value.ToString(); //this is just a helper data. It will never show to the user when the program is done
switch (color) {
case 1:
if(value>=2 && value<=10){
pb??.Image = Projectname.cards_club.(Cards_+VALUE+_Club_svg).ToString();
}
My problem is: how can I use the previously generated number (stored as pb_num) here pb??.Image = , where the question mark is. And here Projectname.cards_club.(Cards_+value+_Club_svg).ToString(); how can I combine a previously generated random number (stored as value) with the name of the picture? So with this I can get a picture in the picturebox, where a random number (for example 5) shows the exact card.
If I get 5 (value = 5) I want to show in the picturebox the Cards-5-Club.svg.
Thank you so much your answers, and please feel free to ask if anything is not exactly clear.
**OK, I could solved the problems. :)**
The first one:
"How can I use the previously generated number (stored as pb_num) here pb??.Image = , where the question mark is. "
The answere was soooo obvious (and this was the easier to find it)
I used this question: Name of picturebox
And the answer, witch marked with a thick. (this: https://stackoverflow.com/a/10934094/20290206)
The second one:
"And here Projectname.cards_club.(Cards_+value+_Club_svg).ToString(); how can I combine a previously generated random number (stored as value) with the name of the picture? "
It was hard to find the answere. :/ But I find! :)
I used this question to solve it: How to retrieve Image from Resources folder of the project in C#
And these TWO answers:
This:
https://stackoverflow.com/a/55959810
and that: https://stackoverflow.com/a/32156875
Because I noticed that I use a different way, to create resource files. So, at first I just clicked with right mouse button on the project name on the Solution Explorer, and choose the Add>>New Item option. In the Pop-up window and I choosed the "Resource Files" option.
This was a good method to store the pictures, and reflect them if you preciselly know their name, and you want to use it "just simply". For example if you want to use program integrated pictures when you set the picturebox-content (see the attached picture!).
My wrong method was this (it is not wrong, just not give me the option of calling a specified picture from the resources):
1th pic
2nd pic
BUT the correct way, if you want to reflect to their name is the method which is in the following link, illustrated with picture: https://stackoverflow.com/a/55959810
Then I just mothified this answere:
https://stackoverflow.com/a/32156875
And I can call the picture which contains the random number. :)
So the code which is WORK :
Random rnd = new Random();
int color = rnd.Next(1,4+1);
int value = rnd.Next(1,13+1);
int pb_num = rnd.Next(1,4+1);
textBox1.Text=color.ToString()+" "+value.ToString(); //this is just a helper data. It will never show to the user when the program is done
switch (color) {
case 1:
if(value>=2 && value<=10){
((PictureBox)this.Controls["pb" + pb_num.ToString()]).Image = (Image)Properties.Resources.ResourceManager.GetObject("Cards_" + value + "_Club_svg");
}
I hope this will help someone how struggle with similar problems. :)
I am working on a small-project similar to popcorn time, but I have faced some issues.
First of all when I call the function which gets all available movies and adds them to FlowLayoutPanel with its cover photos it uses a huge amount of ram like 3 GB! so is there any way to fix this (this == the below code)? (Note that I have checked the function that gets all movies from database alone and it uses only 43MB.)
List<TVShows> T1 = TVShows.GetAll();
TVShowsFlowPanel.Controls.Clear();
for (int i = 0; i < T1.Count; i++)
{
TVShowControl P1 = new TVShowControl(T1[i]);
TVShowsFlowPanel.Controls.Add(P1);
}
second, when I type PictureBox1.picture=http://..... to get the cover picture is there a way to ask if that picture was fully downloaded and showed before I move to the next Movie? and why it takes a long time to get the pic even I am using popcorn time same API with the same internet connection
last, when I add like 80 movies in that flow layout panel, why it's not smooth while scrolling down even the loading process was finished, I mean the pictures show some irritating, random lines until I stay in a specific spot.
Thanks for any help I do really appreciate that!
I'm creating a card game similar to Play Your Cards Right using C# WFA.
I have 52 card faces as PNGs that I want to be able to apply to pictureboxes. I have 10 picture boxes, pbP1Card1 -> pbP1Card5 and pbP2Card1 -> pbP2Card5. When a card is drawn, I want to change the next picturebox from a cardback image to the corresponding card face for the newly drawn card.
I am trying to find a fast solution to getting this to work across all ten PBs. I am thinking of a method similar to this:
string picbox = "pbP1Card1";
string card = "ace_of_spades";
is used in
picbox.Image = Properties.Resources.card
Where the picbox being targeted and the current card can be changed accordingly.
EDIT:
Along with Joe's answer, I have achieved what I wanted by using the following:
picbox.Image = ((System.Drawing.Image)(Properties.Resources.ResourceManager.GetObject(card)));
string picbox = "pbP1Card1";
picbox.Image = Properties.Resources.card
That's not gonna work at all the way you want. Using string values as variable name references is not good practice. Instead, define your PictureBox controls with an array, like this:
PictureBox[,] cards = new PictureBox[1,4];
Now, pbP1Card1 will be cards[0,0]. pbP2Card5 would be cards[1,4], and you can use integer variables to increment through the positions. So you might have a line of code somewhere that looks like this:
cards[0,i].Image = Properties.Resources.card
The best thing here is you don't have to remove and recreate your existing PictureBoxes. Instead, your initialization code has a few lines that look like this:
cards[0,0] = pbP1Card1;
cards[0,1] = pbP1Card2;
cards[0,2] = pbP1Card3;
//...
cards[1,4] = pbP2Card4;
Since PictureBox is a reference type, you will be able to access the same PictureBox object using either reference, so anything you've already written to use the existing names will still work.
Please help me understand how to interpret histograms and calculate bins to be put in generating histograms. First of all, I am still a student and I am confused on the things I have read from my research. I have read that to calculate a bin, you need to know the range of the data(dataset?). My problem is that I do not know how to get the data and the range of data in my image.
The DenseHistogram asks for these parameters....
DenseHistogram x = new DenseHistogram(int binSize, RangeF range);
My code is below and I just copied this from another person here so I can try it and I really want to understand how he got 256 for binSize and 0-256 for range
DenseHistogram hist = new DenseHistogram(256, new RangeF(0, 256));
and
my problem here Ma'am/Sir, is that my histogram looks different from other histograms I have seen on the internet and, again, the binSize.
the histogram looks like click here to view histogram and this is the image used for generateHistograms click here to view image , it is different from others and I have noticed that if I changed the number of bins, the lines also change. This is why I want to know how many bins I should put in....
histogramBox.generateHistograms(IImage image, int numberOfBins);
Please help me understand and can you please explain why the histogram looked like that? THANK YOU SO MUCH FOR YOUR HELP!!!
How can I add an item to a list if that item is essentially a pointer and avoid changing every item in my list to the newest instance of that item?
Here's what I mean:
I am doing image processing, and there is a chance that I will need to deal with images that come in faster than I can process (for a short period of time). After this "burst" of images I will rely on the fact that I can process faster than the average image rate, and will "catch-up" eventually.
So, what I want to do is put my images into a <List> when I acquire them, then if my processing thread isn't busy, I can take an image from that list and hand it over.
My issue is that I am worried that since I am adding the image "Image1" to the list, then filling "Image1" with a new image (during the next image acquisition) I will be replacing the image stored in the list with the new image as well (as the image variable is actually just a pointer).
So, my code looks a little like this:
while (!exitcondition)
{
if(ImageAvailabe())
{
Image1 = AcquireImage();
ImgList.Add(Image1);
}
if(ImgList.Count > 0)
{
ProcessEngine.NewImage(ImgList[0]);
ImgList.RemoveAt(0);
}
}
Given the above, how can I ensure that:
- I don't replace all items in the list every time Image1 is modified.
- I don't need to pre-declare a number of images in order to do this kind of processing.
- I don't create a memory devouring monster.
Any advice is greatly appreciated.
Just reinitialize:
Replace
Image1 = AcquireImage();
with
Image1 = new Image(AcquireImage());
or just say
ImageList.Add(new Image(AcquireImage()));
Your code is correct. None of your above code affects previously added images. The line:
Image1 = AcquireImage();
puts the reference (to an image) returned from AcquireImage into the Image1 reference variable. Then:
ImgList.Add(Image1);
adds that reference to your list. Changing your Image1 reference variable does not affect references already in the list.
Conceptually, your code will be fine. The important element is that AcquireImage() allocates a new instance for each incoming image.
If Image1 were a pointer, you would have a problem - however a C# reference is not a pointer.
If I understand what you're saying correctly, you want to be able to re-use a variable without overwriting its existing data. The good news is that you don't need to change anything. You're partially correct when you say that Image1 is a pointer: it's a reference to whichever image it's pointing to at the time. When you allocate it:
Image1 = AcquireImage();
you're not overwriting the contents of the existing image, but changing the reference so it points to the new image. Assuming AcquireImage is working correctly and returns a new image every time, rather than overwriting the previous one, the above code will discard the existing reference in favour of the new one. However, as you've added it to the list already, a reference to the image is retained somewhere in your code, and so it will not be lost.