Show certain number of datapoints on chart at a time - winforms

I'm new to charting in winforms and I'm having a little trouble getting the chart to display a set number of points at time.
Using the code from this answer https://stackoverflow.com/a/5146430 :
private void FillChart()
{
int blockSize = 100;
// generates random data (i.e. 30 * blockSize random numbers)
Random rand = new Random();
var valuesArray = Enumerable.Range(0, blockSize * 30).Select(x => rand.Next(1, 10)).ToArray();
// clear the chart
chart1.Series.Clear();
// fill the chart
var series = chart1.Series.Add("My Series");
series.ChartType = SeriesChartType.Line;
series.XValueType = ChartValueType.Int32;
for (int i = 0; i < valuesArray.Length; i++)
series.Points.AddXY(i, valuesArray[i]);
var chartArea = chart1.ChartAreas[series.ChartArea];
// set view range to [0,max]
chartArea.AxisX.Minimum = 0;
chartArea.AxisX.Maximum = valuesArray.Length;
// enable autoscroll
chartArea.CursorX.AutoScroll = true;
// let's zoom to [0,blockSize] (e.g. [0,100])
chartArea.AxisX.ScaleView.Zoomable = true;
chartArea.AxisX.ScaleView.SizeType = DateTimeIntervalType.Number;
int position = 0;
int size = blockSize;
chartArea.AxisX.ScaleView.Zoom(position, size);
// disable zoom-reset button (only scrollbar's arrows are available)
chartArea.AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.SmallScroll;
// set scrollbar small change to blockSize (e.g. 100)
chartArea.AxisX.ScaleView.SmallScrollSize = blockSize;
}
What would I change here to only show, say three data points at a time? I tried changing the Maximum of the x-axis, which worked but took away the scrolling bar. I also tried messing around with the blockSize variable here.

Related

Keep tablelayoputpanel rows height identical as rows are added dynamically

I have a TableLayoutPanel whose rows are set to autoresize. It has two columns, but rows can be added dynamically at run time.
How do I add a row, and then have the height of each roenter code herew be the same percentage? I tried this code after adding the control:
private void BindRawData(int rc) // row number is passed in
{
var percent = 100 - (tableLayoutPanel1.RowCount * 10);
tableLayoutPanel2.Controls.Add(grid1, 1, rc);
for(int i = 0; i < tableLayoutPanel2.RowStyles.Count - 1; i++)
{
var panel = tableLayoutPanel2.RowStyles[i];
panel.SizeType = SizeType.AutoSize;
}
}
The result is a grid on top that is really tiny, and the next grid takes up almost the whole panel.

How can I fix this error : 'Cannot invoke loadPixels() on the array type PImage[]'?

I'm kind of a beginner with Processing and I've been having difficulty with my pixel array. I have 10 images numbered from 0-9 that are being shown one after another... What I am trying to do on top of that is take each image and change its brightness level, turning it either white or black.
I've already tried to just change the brightness using one image, so not an array of images which works perfectly! But when joining those two together it doesn't work for me.
int maxImages = 10; // The number of frames in the animation
int imageIndex = 00; //initial image to be displayed first
PImage[] picture = new PImage[maxImages]; //the image array
void setup() {
size(500, 500); //size of sketch
frameRate(1); //frames processed per second
//loading images with numbered files into the array
for (int i = 0; i< picture.length; i++) {
picture[i] = loadImage("spast_" + i + ".jpg");
}
}
void draw() {
image(picture[imageIndex], 0, 0); //dispaying one image
loadPixels(); //accessing pixels
picture.loadPixels(); //accessing the image pixels too
//THIS is where it stops me and gives me 'Cannot invoke loadPixels() on the array type PImage[]'
for (int x = 0; x < width; x++) { //loops through every single x value
for (int y = 0; y < height; y++) { //loops through every single y value
int loc = x + y*width; // declare integer loc
float b = brightness(picture.pixels[loc]); //give me the brightness of pixels
if (b < 150) { //if the pixel is lower than 150
pixels[loc] = color(0); //then make those pixels black
} else { //otherwise
pixels[loc] = color(255); //make pixels white
}
}
}
imageIndex = (imageIndex + 1) % picture.length; //increment image index by one each cycle
updatePixels(); //when finished with the pixel array update the pixels
}
I'm expecting as each image is displayed it brightness value is changed and then it goes on to image 2 and so on...
Your picture variable is an array of PImage instances. You can't call loadPixels() on the array itself. You have to get a PImage at a specific index of the array, and then call the loadPixels() function on that instance.
Here's an example:
picture[0].loadPixels();
This line of code gets the PImage at index 0 and calls the loadPixels() function on it.
Note that you're already doing something pretty similar with this line:
image(picture[imageIndex], 0, 0);
Shameless self-promotion: here is a tutorial on arrays in Processing.
This is the updated and working code:
int maxImages = 10; // The number of frames in the animation
int imageIndex = 0; //initial image to be displayed first
PImage[] picture = new PImage[maxImages]; //the image array
void setup() {
size(500, 500); //size of sketch
frameRate(1); //frames processed per second
//loading images with numbered files into the array
for (int i = 0; i< picture.length; i++) {
picture[i] = loadImage("spast_" + i + ".jpg");
}
}
void draw() {
loadPixels(); //accessing pixels
image(picture[imageIndex], 0, 0); //dispaying one image
imageIndex = (imageIndex + 1) % picture.length; //increment image index by one each cycle
picture[imageIndex].loadPixels(); //accessing the image pixels in the array
for (int x = 0; x < width; x++) { //loops through every single x value
for (int y = 0; y < height; y++) { //loops through every single y value
int loc = x + y*width; // declare integer loc
float b = brightness(picture[imageIndex].pixels[loc]); //give me the brightness of pixels
if (b < 150) { //if the pixel is lower than 150
pixels[loc] = color(0); //then make those pixels black
} else { //otherwise
pixels[loc] = color(255); //make pixels white
}
}
}
updatePixels(); //when finished with the pixel array update the pixels
}

Set Width of XRPictureBox at Runtime

I am creating devexpress xtrareport's xrpicturebox as below.
if (dtPath.Rows.Count <= 0) return;
var panelResimler = new XRPanel {Width = 800};
float x = 0;
float y = 0;
for (var i = 0; i < dtPath.Rows.Count; i++)
{
XRPictureBox xrPictureBox1 = new XRPictureBox
{
ImageUrl = dtPath.Rows[i]["DokumanYolu"].ToString(),
Sizing = ImageSizeMode.AutoSize,
Dpi = 100F,
LocationFloat = new DevExpress.Utils.PointFloat(x, y),
Name = "xrPictureBox1",
};
//if (xrPictureBox1.Width > 800)
//{
// xrPictureBox1.Width = 800;
//}
if (xrPictureBox1.Right > 800)
{
xrPictureBox1.Left = 0;
}
else
{
xrPictureBox1.Top = 0;
}
if (xrPictureBox1.Bottom > y) y = xrPictureBox1.Bottom;
x = xrPictureBox1.Right;
panelResimler.Controls.Add(xrPictureBox1);
}
Detail2.Controls.Add(panelResimler);
The problem is, when I use panel as here image part is not getting visible while size doesnt fit to the report. BTW the comment out part does not help here.
Othervise if I wont use panel that time image streching to severap pages.
are there any solution for limiting image width by page width?
This is what I desire
This is without panel
Thans in advance...
ImageSizeMode.AutoSize mode will not set specific size for control - it will be corrected on printing / document generation process. so you should not look at the control size (Right and Bottom properties), you can change the picture size as a page size divided to count of pictures in line (if i correctly understand your scenario) and set the PictureBox Sizing mode to Zoom image or Squeeze (or smth similar).

c# WIA 12 bit grayscale image loses quality as bitmap

I am writing a windows forms application. I have an Image, received from CCD camera. The camera takes 12 bit grayscale tiff – 2 bytes per pixel. To receive it I am using WIA(windows image acquisition). I can make a byte array from the image data. After that I need to display the image in picturebox. When shown in picturebox, the image is too dark. Also, the created bitmap is Format32bppArgb. Do I lose quality, when creating the bitmap and how can I make the needed bitmap format? I am extremely new to image processing and any help will be great. I have read many forum posts on the topic of conversion between the formats, but with no luck so far.
So any ideas how to receive 16 bit grayscale bitmap from what I have so far?
Edit: This now is working:
private void btnAdvancedTestSnap_Click(object sender, EventArgs e)
{
WIA.CommonDialog _dialog = new CommonDialogClass();
WIA.Device _camera = _dialog.ShowSelectDevice(WIA.WiaDeviceType.CameraDeviceType, false, false);
ImageFile imageFile = (ImageFile)_camera.Items[1].Transfer(EnvFormatID.wiaFormatTIFF);
Byte[] receivedBytes = (byte[])imageFile.FileData.get_BinaryData();
int bytecount = receivedBytes.Length;
int width = imageFile.Width;
int height = imageFile.Height;
int dimension = width * height * 2;//number of bytes representing the image - 2 bytes per pixel
int startImgBytes = bytecount - dimension;
startImgBytes += 1;// from which position of the big array to start making pixel values
byte[] imageBytes = new byte[dimension]; //byte array with only bytes,representing the picture
int j = 0;
for (int i = startImgBytes; i < receivedBytes.Length; i++)//filling the only pixel byte data array
{
imageBytes[j] = receivedBytes[i];
j++;
}
int pixDimension = width * height; //number of pixels in the image
int[] pixVal = new int[pixDimension];
int z = 0;
for (int i = 0; i < imageBytes.Length; i+=2)
{
int res = (imageBytes[i] * 0x100) + imageBytes[i + 1];//int value of the pixel, 2 bytes per pixel
int pix = (res * 255) / 4095;// scalling down to 8 bit value
pixVal[z] = pix;
z++;
}
Bitmap newImage = new Bitmap(width, height, PixelFormat.Format24bppRgb);
Color p;
//grayscale
int counter = 0;
for (int y = 0; y < height; y++)//height
{
for (int x = 0; x < width; x++)//width
{
int val = pixVal[counter];
newImage.SetPixel(x,y,Color.FromArgb(255,val,val,val));
counter++;
}
}
pbAdvanced.Image = newImage; //show the image in picture box
But this method is very slow for high resolution. Any ideas how to improve the speed. I read examples with Marshal.Copy and lockbits, but in all examples they use a source image and copy to new. Any help will be greately appreciated

Creating a resizable grid in WPF

I need to write a C# WPF program in order to let the user individually modify the width and height of a grid using the mouse. After some reading, I've found out that WPF featues the GridSplitter control, which seems to be a possible solution for my problem. So far, this is my approach:
private const int NumCols = 5;
private const int NumRows = 7;
private void CreateDynamicWPFGrid()
{
// Create the Grid
var dynamicGrid = new Grid();
for (int i = 0; i < NumCols - 1; ++i )
{
// Define 2 * (NumCols - 1) columns. For every two columns, the first one will hold a label
// whereas the second one will hold a vertical splitter.
var gridColDefA = new ColumnDefinition();
// The gridColDefB is for the splitter.
var gridColDefB = new ColumnDefinition();
gridColDefB.Width = new GridLength(1, GridUnitType.Auto);
dynamicGrid.ColumnDefinitions.Add(gridColDefA);
dynamicGrid.ColumnDefinitions.Add(gridColDefB);
}
{
// The last column only needs a cell for holding a label. No splitter whatsoever.
var gridColDef = new ColumnDefinition();
dynamicGrid.ColumnDefinitions.Add(gridColDef);
}
for (int j = 0; j < NumRows - 1; ++j)
{
var gridRowDefA = new RowDefinition();
var gridRowDefB = new RowDefinition();
// The gridRowDefB is for the splitter.
gridRowDefB.Height = new GridLength(1, GridUnitType.Auto);
dynamicGrid.RowDefinitions.Add(gridRowDefA);
dynamicGrid.RowDefinitions.Add(gridRowDefB);
}
{
// The last row only needs a cell for holding a label. No splitter whatsoever.
var gridRowDef = new RowDefinition();
dynamicGrid.RowDefinitions.Add(gridRowDef);
}
for (int i = 0; i < NumCols - 1; ++i )
{
for(int j = 0; j < NumRows - 1; ++j )
{
// Insert the label.
var label = new Label();
label.Content = "C" + i + "-R" + j;
label.Background = new SolidColorBrush(Colors.Azure);
Grid.SetColumn(label, 2 * i);
Grid.SetRow(label, 2 * j);
dynamicGrid.Children.Add(label);
// Insert the horizontal splitter.
var horizontalGridSplitter = new GridSplitter();
horizontalGridSplitter.Height = 1;
horizontalGridSplitter.Background = new SolidColorBrush(Colors.DarkSlateBlue);
horizontalGridSplitter.HorizontalAlignment = HorizontalAlignment.Stretch;
horizontalGridSplitter.VerticalAlignment = VerticalAlignment.Center;
Grid.SetColumn(horizontalGridSplitter, 2 * i );
Grid.SetRow(horizontalGridSplitter, 2 * j + 1);
Grid.SetRowSpan(horizontalGridSplitter, 1);
Grid.SetColumnSpan(horizontalGridSplitter, 1);
dynamicGrid.Children.Add(horizontalGridSplitter);
// Insert the vertical splitter.
var verticalGridSplitter = new GridSplitter();
verticalGridSplitter.Width = 1;
verticalGridSplitter.Background = new SolidColorBrush(Colors.DarkSlateBlue);
verticalGridSplitter.HorizontalAlignment = HorizontalAlignment.Center;
verticalGridSplitter.VerticalAlignment = VerticalAlignment.Stretch;
Grid.SetColumn(verticalGridSplitter, 2 * i + 1);
Grid.SetRow(verticalGridSplitter, 2 * j + 1);
Grid.SetRowSpan(verticalGridSplitter, 1);
Grid.SetColumnSpan(verticalGridSplitter, 1);
dynamicGrid.Children.Add(verticalGridSplitter);
}
}
// Display grid into a Window
Content = dynamicGrid;
}
The output I'm getting is as follows:
Notice that I'm only able to resize the rows (don't know why the vertical splitters don't show up) and, for some reason, when I grab a horizontal splitter it resizes the whole row and not just the individual cell. Any ideas? Please, See the following screenshot to see the resizing in action:
This is what I'd expect if I resize cell (0,0) (the image has being manually edited by me):
Thanks in advance!
If you remove the line setting the row for your verticalGridSplitter and set them to span NumRows, you will see your vertical splitters. But ultimately, I think you are trying to do something that the grid with splitters can't do. You can't resize the width and height of individual cells, only whole rows and columns.
After all, if you make C0-R0 taller, what do you expect the rest of that row to do?

Resources