This generates a picturebox
PictureBox[][] picturebox;
public void loadPictureBox()
{
string path = #"../../Images/Catelogue/"; //set pathing
string[] list = Directory.GetFiles(path, "*.jpg");
//pictureboxCatelogue = new PictureBox[list.Length];
//pictureboxCosplay = new PictureBox[list.Length];
picturebox = new PictureBox[4][];
for (int i = 0; i < 4; i++)
{
picturebox[i] = new PictureBox[list.Length];
int y = 85, temp = 220, run = 0;
for (int index = 13; index < list.Length; index++) // loads all pictures and create pictureboxes
{
picturebox[i][index] = new PictureBox();
picturebox[i][index].Image = Image.FromFile(path + index + ".jpg");
this.Controls.Add(picturebox[i][index]);
temp = temp + 200;
if (index % 4 == 0)
{
if (run != 0)
y = y + 200;
run++;
temp = 220;
}
picturebox[i][index].Location = new Point(temp, y);
picturebox[i][index].Size = new Size(180, 180);
picturebox[i][index].Name = Convert.ToString(index);
picturebox[i][index].SizeMode = PictureBoxSizeMode.Zoom;
picturebox[i][index].BackColor = Color.FromArgb(35, 35, 35);
picturebox[i][index].Click += new System.EventHandler(PictureBox_Click);
}
}
}
I'm trying to hide a jagged array which is a picturebox in winsform c#, but i keep getting an error, is hiding a jagged array possible? This is the code that i'm having trouble with.
for (int i = 0; i < picturebox.Length; i++)
{
picturebox[0][i].Hide();
}
This is the error i get
ERROR : A first chance exception of type 'System.NullReferenceException' occurred in APPD Assignment 2.exe (Additional information: Object reference not set to an instance of an object.)
Aren't you using the wrong length here?
for (int i = 0; i < picturebox.Length; i++)
Should be
for (int i = 0; i < picturebox[0].Length; i++)
When you load the pictureboxes, you're only starting at index 13, so you either need to start the hide loop at 13, or check for null.
for (int i = 13; i < picturebox[0].Length; i++)
{ ... }
or
for (int i = 0; i < picturebox[0].Length; i++)
{
if (picturebox[0][i] != null)
{
picturebox[0][i].Hide();
}
}
Related
I want to remove a specific object from an array, put it in a smaller array without getting out of range. This is what I've tried but it won't work.
Skateboard[] newSkateboard = new Skateboard[_skateboards.Length - 1];
for (int i = 0; i < _skateboards.Length; i++)
{
if (skateboard.Code != _skateboards[i].Code)
{
newSkateboard[i] = _skateboards[i];
}
}
Sure.
var j = 0;
for (int i = 0; i < _skateboards.Length; i++)
{
if (skateboard.Code != _skateboards[i].Code)
{
newSkateboard[j] = _skateboards[i];
j = j + 1;
}
}
So I am making a graph of the average between to lists of coordinates.
So I have been looking all over and can't seem to find any information on how I can find the average of the 2 lists of values. When I try I get an error "the index was outside the matrix boundaries" and when I got it to work I just made a graph where the years where extremely high and the graph itself were looking insane. What i do is importing 2 parts of(data/data2) information with Json.
//
// Data
//
int tal = dataSet.dataset.value.Count;
//Add items in the listview
int[] yData = new int[tal];
int[] xData = new int[tal];
int k = 0;
foreach (var item in dataSet.dataset.dimension.Tid.category.label)
{
xData[k++] = int.Parse(item.Value.ToString());
}
for (int i = 0; i < tal; i++)
{
yData[i] = dataSet.dataset.value[i];
}
//
// Data2
//
int tal2 = dataSet2.dataset.value.Count;
int[] y2Data = new int[tal2];
int[] x2Data = new int[tal2];
int j = 0;
foreach (var item in
dataSet2.dataset.dimension.Tid.category.label)
{
x2Data[j++] = int.Parse(item.Value.ToString());
}
for (int p = 0; p < tal2; p++)
{
y2Data[p] = dataSet2.dataset.value[p];
}
This is the part
///////////////////////////////////////////////////////////
int[] ySum = new int[xData.Length];
for (int i = 0; i < xData.Length; i++)
{
ySum[i] = (yData[i] + y2Data[i]) / 2;
}
///////////////////////////////////////////////////////////
List<int> GenUd = new List<int>(yData.ToList());
textBoxGenUd.Text = GenUd.Average().ToString();
List<int> GenInd = new List<int>(y2Data.ToList());
textBoxGenInd.Text = GenInd.Average().ToString();
chartArea1.Name = "ChartArea1";
chart2.ChartAreas.Add(chartArea1);
chart2.Dock = DockStyle.Fill;
for (int i = 0; i <xData.Count(); i++)
{
series1.Points.AddXY(ySum[i], x2Data[i]);
}
MySecChart2 mc3 = new MySecChart2(series1);
mc3.ShowDialog();
mine is just an educated guess - if the exception occurs at the following:
This is the part
///////////////////////////////////////////////////////////
int[] ySum = new int[xData.Length];
for (int i = 0; i < xData.Length; i++)
{
ySum[i] = (yData[i] + y2Data[i]) / 2;
}
///////////////////////////////////////////////////////////
My diagnosis would be that yData[i] and y2Data[i] they dont have the same length and of xData.Length define into the loop definition.
Was perhaps supposed to be yData.Length in the loop definition?
Ive been working on a code that takes an image, turns it into grayscale, and then does image manipulation depending on which button is pressed. When a button is pressed (ex. Average 3x3, Prewitt 5x5), it calls a 2D Multiplication function, which loops over the grayscale image, while looping over the kernel, adding all the values in the matrix. If any value is over 255, it sets it to 255. Then using the SetPixel on a temporary bitmap variable, which is finally put into the picturebox. When i run the program, I select an image and it shows it (as grayscale), but after choosing one of the filters, the program freezes for around 30 seconds, and then nothing changes, no filter is applied. Ive tried debugging and i cant seem to locate what the problem is!
EDIT: The initial question has been solved( I had to refresh the picturebox for the new image to show properly.
But i am facing another problem here with regards to the prewitt kernel.
i get this error
"Additional information: Value of '-6' is not valid for 'red'. 'red' should be greater than or equal to 0 and less than or equal to 255."
And i am not sure what to change in my code to fix this.
Initializing:
public partial class Form1 : Form
{
private Image img;
Bitmap grayscaleimage;
double[][] AVGKernel = new double[11][];
double[][] PrewittKernel = new double[11][];
int[] AVGKernal1DH = new int[11];
int[] AVGKernal1DV = new int[11];
Bitmap tempBitmap;
public Form1()
{
InitializeComponent();
for (int i = 0; i < 11; i++)
{
AVGKernel[i] = new double[11];
PrewittKernel[i] = new double[11];
for (int j = 0; j < 11; j++)
{
AVGKernel[i][j] = 0;
PrewittKernel[i][j] = 0;
AVGKernal1DH[j] = 0;
AVGKernal1DV[j] = 0;
}
}
}
The open button and turning the picture into grayscale:
private void OpenImageButton(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
this.img = Image.FromFile(openFileDialog.FileName);
grayscaleimage = new Bitmap(img);
int rgb;
Color c;
for (int y = 0; y < grayscaleimage.Height; y++)
for (int x = 0; x < grayscaleimage.Width; x++)
{
c = grayscaleimage.GetPixel(x, y);
rgb = (int)((c.R + c.G + c.B) / 3);
grayscaleimage.SetPixel(x, y, Color.FromArgb(rgb, rgb, rgb));
}
this.pictureBox1.BackgroundImage = grayscaleimage;
pictureBox1.BackgroundImageLayout = ImageLayout.Zoom;
}
}
an example of the many buttons available:
private void button5_Click(object sender, EventArgs e)
{
AVGKernel[0][0] = 1; AVGKernel[0][1] = 1; AVGKernel[0][2] = 1; AVGKernel[0][3] = 1; AVGKernel[0][4] = 1;
AVGKernel[1][0] = 1; AVGKernel[1][1] = 1; AVGKernel[1][2] = 1; AVGKernel[1][3] = 1; AVGKernel[1][4] = 1;
AVGKernel[2][0] = 1; AVGKernel[2][1] = 1; AVGKernel[2][2] = 1; AVGKernel[2][3] = 1; AVGKernel[2][4] = 1;
AVGKernel[3][0] = 1; AVGKernel[3][1] = 1; AVGKernel[3][2] = 1; AVGKernel[3][3] = 1; AVGKernel[3][4] = 1;
AVGKernel[4][0] = 1; AVGKernel[4][1] = 1; AVGKernel[4][2] = 1; AVGKernel[4][3] = 1; AVGKernel[4][4] = 1;
kernal2DMultiplication(AVGKernel, 5);
this.pictureBox1.BackgroundImage = tempBitmap;
}
Prewitt 5x5 kernel
private void button13_Click(object sender, EventArgs e)
{
PrewittKernel[0][0] = 2; PrewittKernel[0][1] = 1; PrewittKernel[0][2] = 0; PrewittKernel[0][3] = -1; PrewittKernel[0][4] = -2;
PrewittKernel[1][0] = 2; PrewittKernel[1][1] = 1; PrewittKernel[1][2] = 0; PrewittKernel[1][3] = -1; PrewittKernel[1][4] = -2;
PrewittKernel[2][0] = 2; PrewittKernel[2][1] = 1; PrewittKernel[2][2] = 0; PrewittKernel[2][3] = -1; PrewittKernel[2][4] = -2;
PrewittKernel[3][0] = 2; PrewittKernel[3][1] = 1; PrewittKernel[3][2] = 0; PrewittKernel[3][3] = -1; PrewittKernel[3][4] = -2;
PrewittKernel[4][0] = 2; PrewittKernel[4][1] = 1; PrewittKernel[4][2] = 0; PrewittKernel[4][3] = -1; PrewittKernel[4][4] = -2;
kernal2DMultiplication(PrewittKernel, 5);
this.pictureBox1.BackgroundImage = tempBitmap;
this.pictureBox1.Refresh();
}
and finally, the function being called:
private void kernal2DMultiplication(double[][] kernel, int size)
{
tempBitmap = grayscaleimage;
double nrgb = 0;
for (int i = 0; i < grayscaleimage.Width - size / 2; i++)
{
for (int j = 0; j < grayscaleimage.Height - size / 2; j++)
{
if (i >= size / 2 && j >= size / 2)
{
for (int k = 0; k < size; k++)
for (int l = 0; l < size; l++)
nrgb += grayscaleimage.GetPixel(i + k - (size / 2), j + l - (size / 2)).R * kernel[k][l];
nrgb = nrgb / (size * size);
if (nrgb > 255)
nrgb = 255;
tempBitmap.SetPixel(i, j, Color.FromArgb((int)nrgb, (int)nrgb, (int)nrgb));
}
}
}
}
The problem is that you are modifying the BackgroundImage bitmap in-place rather than copying, modifying the copy, and then setting the copy to be the BackgroundImage:
this.pictureBox1.BackgroundImage = grayscaleimage; // initially
tempBitmap = grayscaleimage;
// Make changes to tempBitmap
this.pictureBox1.BackgroundImage = tempBitmap; // actually still the same pointer
The BackgroundImage setter is not forcing a redraw of the control in this case. To force this yourself, call Refresh():
this.pictureBox1.Refresh();
To improver performance, look into replacing multiple calls to SetPixel with a single call to LockBits. See e.g. here.
I've run into a real headache here.
I have a small program, which displays some images stored on disk. 8 images are displayed in a listview at a time, but when the images are large enough, the memory use (according to the task manager) reaches more than 1300mb! I suspect that there are some images or something which aren't deallocated, but I seem to be unable to pinpoint exactly where. I have tried both disposing all images in listview.largeimagelist.images, tried Clear()'ing the imagelist, but it doesn't make a difference at all.
Here is the current code:
private void btnLoadNewImages_Click(object sender, EventArgs e)
{
int k = lsvImgResult.Items.Count;
for (k = lsvImgResult.Items.Count; k >= 1; k--)
{
Seen.Push((MyFile)lsvImgResult.Items[k - 1].Tag);
imageList.Images.Clear();
}
int i = 0;
lsvImgResult.Items.Clear();
DisplayedImages.Clear();
imageList.Images.Clear();
imageList.ImageSize = new Size(100, 100);
imageList.ColorDepth = ColorDepth.Depth32Bit;
int HowMany = 0;
if (UnSeen.Count >= 8)
{
HowMany = 8;
}
else
{
HowMany = UnSeen.Count;
}
for (i = 1; i <= HowMany; i++)
{
MyFile CurFile = UnSeen.Pop();
Image j = Image.FromFile(CurFile.Filename);
DisplayedImages.Enqueue(CurFile);
imageList.Images.Add(j);
}
lsvImgResult.LargeImageList = imageList;
for (int j = 0; j < imageList.Images.Count; j++)
{
ListViewItem lstItem = new ListViewItem();
lstItem.ImageIndex = j;
lstItem.Tag = DisplayedImages.Dequeue();
lstItem.ToolTipText = ((MyFile)lstItem.Tag).Filename;
lsvImgResult.Items.Add(lstItem);
}
tabImagesLeft.Text = "Images left: " + UnSeen.Count;
}
I want to draw 16*64 matrix, each one containing a:
Microsoft.VisualBasic.PowerPacks.OvalShape.
I used this:
List<Microsoft.VisualBasic.PowerPacks.OvalShape> ovalShape =
new List<Microsoft.VisualBasic.PowerPacks.OvalShape>();
for (int i = 0; i < 16; i++)
{
for (int j = 0; j < 64; j++)
{
OvalShape ovl = new OvalShape();
ovl.Width = 20;
ovl.Height = 20;
ovl.FillStyle = FillStyle.Solid;
ovl.FillColor = Color.Green;
ovalShape.Add(ovl);
}
}
How can I show it in the Window?
Creating a separate container for each shape is not required. Also you can skip the additional container list for the shapes. So you could use this very compact code.
var ovalShapes = new Microsoft.VisualBasic.PowerPacks.ShapeContainer()
{
Dock = DockStyle.Fill,
Margin = new Padding(0),
Padding = new Padding(0),
};
for (int i = 0; i < 16; i++)
for (int j = 0; j < 64; j++)
ovalShapes.Shapes.Add(
new Microsoft.VisualBasic.PowerPacks.OvalShape()
{
Width = 20,
Height = 20,
FillStyle = Microsoft.VisualBasic.PowerPacks.FillStyle.Solid,
FillColor = Color.Green,
Location = new Point(20 * i, 20 * j),
});
this.Controls.Add(ovalShapes);
From MSDN:
An OvalShape control cannot be displayed directly on a form or
container control; it must be contained in a ShapeContainer object.
After you initialize an OvalShape, you will have to set its Parent
property either to an existing ShapeContainer or to a new instance of
ShapeContainer.
Try to set Location and add your controls to the Form:
List<Microsoft.VisualBasic.PowerPacks.OvalShape> ovalShape = new List<Microsoft.VisualBasic.PowerPacks.OvalShape>();
for (int i = 0; i < 16; i++)
{
for (int j = 0; j < 64; j++)
{
OvalShape ovl = new OvalShape();
ovl.Width = 20;
ovl.Height = 20;
ovl.FillStyle = FillStyle.Solid;
ovl.FillColor = Color.Green;
ovl.Location = new Point(ovl.Width*i, ovl.Height*j);
ovalShape.Add(ovl);
}
}
foreach(OvalShape os in ovalShape)
{
Microsoft.VisualBasic.PowerPacks.ShapeContainer shapeContainer = new Microsoft.VisualBasic.PowerPacks.ShapeContainer();
Control c = new Control();
shapeContainer.Parent = c;
os.Parent = shapeContainer;
myForm.Controls.Add(c);
}
First simplify
int totalCount = 1024; //16*64
const int shapeWidth =20;
const int shapeHeight = 20;
for (int j = 0; j < totalCount; j++)
{
OvalShape ovl = new OvalShape();
ovl.Width = shapeWidth;
ovl.Height = shapeHeight;
ovl.FillStyle = FillStyle.Solid;
ovl.FillColor = Color.Green;
ovalShape.Add(ovl);
}
After pickup your drawing area and decide how much shapes per row you would like to have.
So some hypothetical code could look like this:
int shapesPerRowCount = 5;
int yPos = 0;
for(int i=0;i<ovalShape.Count;i++)
{
if(i % shapesPerRowCount == 0) //reach end of the row, so offset Y position by Height
yPos += shapeHeight;
int xPos = i*shapeWidth;
DrawShapeAtPos(ovalShape[i], xPos, yPos); //some special function that draws the shape
}
A very generic code, but just to provide you an idea.
If it's not what you're searching for, please clarify.