c# - Convert Simpleitk image into BItmap. System.ArgumentException -
i want read dicom image using simpleitk, convert bitmap , display result in picturebox. when i'm trying this, argumentexception thrown. how can solve this?
here code:
openfiledialog dialog = new openfiledialog(); dialog.title = "open"; dialog.filter = "dicom files (*.dcm;*.dic)|*.dcm;*.dic|all files (*.*)|*.*"; dialog.showdialog(); if (dialog.filename != "") { using (sitk.imagefilereader reader = new sitk.imagefilereader()) { reader.setfilename(dialog.filename); reader.setoutputpixeltype(sitk.pixelidvalueenum.sitkfloat32); sitk.image image = reader.execute(); var castedimage = sitk.simpleitk.cast(image, sitk.pixelidvalueenum.sitkfloat32); var size = castedimage.getsize(); int length = size.aggregate(1, (current, i) => current * (int)i); intptr buffer = castedimage.getbufferasfloat(); // declare array hold bytes of bitmap. byte[] rgbvalues = new byte[length]; // copy rgb values array. marshal.copy(buffer, rgbvalues, 0, length); stream stream = new memorystream(rgbvalues); bitmap newbitmap = new bitmap(stream); //i have tried in way, generated argumentexception //bitmap newbitmap = new bitmap((int)image.getwidth(), (int)image.getheight(), (int)image.getdepth(), pixelformat.format8bppindexed, buffer); obraz.pic.image = newbitmap; } }
thank comments , attempts help. after consultations , own searching on internet solved issue. first problem inadequate representation of pixel image. had change float32 uint8 provide eight-bit pixel.
var castedimage = sitk.simpleitk.cast(image2, sitk.pixelidvalueenum.sitkuint8); then create bitmap using constructor was commented out in question, (int)image.getwidth() instead of (int)image.getdepth().
bitmap newbitmap = new bitmap((int)image.getwidth(), (int)image.getheight(), (int)image.getwidth(), pixelformat.format8bppindexed, buffer); unfortunately, new problem appeared. image, supposed in gray scale, displayed in strange colors. found solution here
colorpalette pal = newbitmap.palette; (int = 0; <= 255; i++) { // create greyscale color table pal.entries[i] = color.fromargb(i, i, i); } newbitmap.palette = pal; // need re-set property force new colorpalette
Comments
Post a Comment