I have an issue with different image sizes and the PictureBox from the Windows forms. The box is 640*480
pixels large. Displaying a vaiety of image sizes works fine, but some image sizes causes the program to crash with Parameter is not valid
exception in the line where I assign the bitmap to the Picturebox:
System::Drawing::Bitmap ^pictureBimage = gcnew System::Drawing::Bitmap(resEnhWidth, resEnhHeight, resEnhWidth * sizeof(char)* 3, System::Drawing::Imaging::PixelFormat::Format24bppRgb, (System::IntPtr) pictureBox24bitLargeImage);
this->pictureBox1->Image = pictureBimage;
this->pictureBox1->Show();
650*600
works, 640*480
as well, but for example 550*630
causes the crash. Does anyone of you know why and how to fix this?
Cheers
EDIT:
This could possibly help solving the problem: The pictureBox24bitLargeImage
is refreshed every time the image is displayed in the pictureBox:
unsigned char* temp = getByteImFromImageData(showThis2, (hScrollBar2->Value)*width*height, width*height, manContrast);
for (int i = 0; i < width * height; i++){
memset(pictureBox24bitImage + 3 * i, *(temp + i), 3);
}
if (draw_Rectangle){
drawRectangle(pictureBox24bitImage, rect_upperLeftCorner, rect_lowerRightCorner, width, height);
}
free(temp);
The temp array contains values between 0 and 255. Is is created with the getByteImFromImageData
function, which takes a double array and rescales the double values between 0 and 255. If a manual contrast is set then it stretches the doule values between user defined values. Here is the source of the function:
unsigned char* getByteImFromImageData(double *image, int start, int size, bool useManContrast){
unsigned char* newim;
newim = (unsigned char*)malloc(size*sizeof(unsigned char));
double mini = +INFINITY;
double maxi = 0;
if (!useManContrast){
for (int i = 0; i < size; i++){
if (*(image + start + i) > maxi) maxi = *(image + start + i);
if (*(image + start + i) < mini) mini = *(image + start + i);
}
for (int i = 0; i < size; i++){
*(newim + i) = (*(image + start + i) - mini) / (maxi - mini) * 255.0;
}
}
else {
mini = contrastMin;
maxi = contrastMax;
double temp;
for (int i = 0; i < size; i++){
temp = (*(image + start + i) - mini) / (maxi - mini) * 255.0;
if (temp>255) temp = 255;
if (temp < 0) temp = 0;
*(newim + i) = (unsigned char) temp;
}
}
return(newim);
}