我遇到了Windows窗体中不同图像大小和PictureBox的问题。该框大640*480
像素。显示各种图像大小的工作正常,但是一些图像大小导致程序在我将位图分配给Picturebox的行中出现Parameter is not valid
异常时崩溃:
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
也有效,640*480
,但例如550*630
导致崩溃。你们中有谁知道为什么以及如何解决这个问题?
干杯
修改:
这可能有助于解决问题:每次在pictureBox中显示图像时都会刷新pictureBox24bitLargeImage
:
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);
临时数组包含0到255之间的值。使用getByteImFromImageData
函数创建,它采用双数组并重新调整0到255之间的双精度值。如果设置了手动对比度,则它会延伸用户定义值之间的doule值。以下是该函数的来源:
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);
}