0

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);
}
mTORjaeger
  • 67
  • 1
  • 7
  • Have you tried a different overload of Bitmap constructor and see if you experience this problem again? – Emi987 Jun 25 '14 at 13:20
  • Are all the images of the same type? – Denise Skidmore Jun 25 '14 at 13:24
  • How would you use another overload of the Bitmap constructor? The images are all of the same type. RGB bitmap with values between 0 and 255. @DeniseSkidmore – mTORjaeger Jun 26 '14 at 14:10
  • Other Bitmap constructors: http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.bitmap(v=vs.110).aspx – Denise Skidmore Jun 26 '14 at 14:17
  • Are you using the image in more than one place? http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.image(v=vs.110).aspx "If you want to use the same image in multiple PictureBox controls, create a clone of the image for each PictureBox. Accessing the same image from multiple controls causes an exception to occur." – Denise Skidmore Jun 26 '14 at 14:22
  • Is the problem the size or the aspect ratio? The non-working example is the opposite orientation of the working examples. Try: 550*600 (smaller than all working images, but opposite orientation) and 650*630 (same orientation as working images, but larger.) – Denise Skidmore Jun 26 '14 at 14:26
  • It seems like the size is the problem. `200*300` pixels works perfectly fine. So does `100*100`. But `99*99` causes the crash `Parameter is not valid.`@DeniseSkidmore – mTORjaeger Jun 26 '14 at 14:40
  • I would be very very grateful if someone could help me out here. This problem is driving me crazy :-/ @DeniseSkidmore ? – mTORjaeger Jun 27 '14 at 08:17
  • You may have found an entirely different problem than the one I told you to look for. I was very specific about the suggested tests. – Denise Skidmore Jun 27 '14 at 14:27
  • http://stackoverflow.com/questions/12680618/exception-parameter-is-not-valid-on-passing-new-image-to-picturebox – Denise Skidmore Jun 27 '14 at 14:34
  • I looks like this is not an uncommon issue. I suggest you search https://www.google.com/webhp?q=picturebox+image+Parameter+is+not+valid and edit your answer with things you have tried that didn't work for you, or if you do find something that works post your own answer. – Denise Skidmore Jun 27 '14 at 14:36

0 Answers0