c# - Edge of the image is cutted out when conver DIBto ImageSource -
i use below code convert dib scanner twain
bitmapsource, found edge of image cutted off. image size different source image. there thing wrong in below code when handel image?
/// <summary> /// managed bitmapsource dib provided low level windows hadle /// /// notes: /// data copied source windows handle can saftely discarded /// when bitmapsource in use. /// /// subset of possible dib forrmats supported. /// /// </summary> /// <param name="dibhandle"></param> /// <returns>a copy of image in managed bitmapsource </returns> /// public static bitmapsource formhdib(intptr dibhandle) { bitmapsource bs = null; intptr bmpptr = intptr.zero; bool flip = true; // vertivcally flip image try { bmpptr = win32.globallock(dibhandle); win32.bitmapinfoheader bmi = new win32.bitmapinfoheader(); marshal.ptrtostructure(bmpptr, bmi); if (bmi.bisizeimage == 0) bmi.bisizeimage = (uint)(((((bmi.biwidth * bmi.bibitcount) + 31) & ~31) >> 3) * bmi.biheight); int palettsize = 0; if (bmi.biclrused != 0) throw new notsupportedexception("dibtobitmap: dib pallet not supported"); // pointer beginning of bitmap bits intptr pixptr = (intptr)((int)bmpptr + bmi.bisize + palettsize); // define parameters used create bitmapsource. pixelformat pf = pixelformats.default; switch (bmi.bibitcount) { case 32: pf = pixelformats.bgr32; break; case 24: pf = pixelformats.bgr24; break; case 8: pf = pixelformats.gray8; break; case 1: pf = pixelformats.blackwhite; break; default: // not supported throw new notsupportedexception("dibtobitmap: can't determine picture format (bibitcount=" + bmi.bibitcount + ")"); // break; } int width = bmi.biwidth; int height = bmi.biheight; int stride = (int)(bmi.bisizeimage / height); byte[] imagebytes = new byte[stride * height]; //debug: initialize image random data. //random value = new random(); //value.nextbytes(rawimage); if (flip) { (int = 0, j = 0, k = (height - 1) * stride; < height; i++, j += stride, k -= stride) marshal.copy(((intptr)((int)pixptr + j)), imagebytes, k, stride); } else { marshal.copy(pixptr, imagebytes, 0, imagebytes.length); } int xdpi = (int)math.round(bmi.bixpelspermeter * 2.54 / 100); // pels per meter dots per inch int ydpi = (int)math.round(bmi.biypelspermeter * 2.54 / 100); // create bitmapsource. bs = bitmapsource.create(width, height, xdpi, ydpi, pf, null, imagebytes, stride); win32.globalunlock(pixptr); win32.globalfree(pixptr); pixptr = intptr.zero; imagebytes = null; bmi = null; } catch (exception ex) { //string msg = ex.message; } { // cleanup if (bmpptr != intptr.zero) { // locked sucsessfully win32.globalunlock(dibhandle); } } win32.globalunlock(bmpptr); win32.globalfree(bmpptr); bmpptr = intptr.zero; return bs; }
this code here.
Comments
Post a Comment