c# - Bitmap with Nearest Neighbor Causes Tearing -
i've created test application generates code 39 barcode. problem when display barcode on screen, either blurs or gets torn. if use bitmapscalingmode
other nearestneighbor
, blurred barcode. when use nearestneighbor
, diagonal slash shown below. diagonal occurs when resize window. (it stays there if stop in right spot.) image not change size, instead moves across screen resize window.
i've tried using renderoptions.edgemode="aliased"
well, doesn't seem have effect...
torn / blurred / correct
wpf sample code:
<border borderbrush="black" borderthickness="1" horizontalalignment="center" verticalalignment="top" margin="0,50,0,0"> <image x:name="imgbarcode" stretch="fill" renderoptions.bitmapscalingmode="highquality" renderoptions.edgemode="aliased" /> </border>
image generation:
imgbarcode.source = loadbitmap(c.generate(barcodetext.text)); imgbarcode.width = c.getwidth(); imgbarcode.height = c.getheight();
sample generation code:
bitmap bmp = new bitmap(width, height); using (graphics gfx = graphics.fromimage(bmp)) using (solidbrush black = new solidbrush(color.black)) using (solidbrush white = new solidbrush(color.white)) { // start barcode: addbar(gfx, black, white, '*'); foreach (char c in barcode) { addcharacter(gfx, black, white, c); } // end barcode: addbar(gfx, black, white, '*'); }
sample rectangle addition:
g.fillrectangle(white, left, top, narrow, height); left += narrow;
load bitmap taken stackoverflow question:
[dllimport("gdi32")] static extern int deleteobject(intptr o); public static bitmapsource loadbitmap(system.drawing.bitmap source) { intptr ip = source.gethbitmap(); bitmapsource bs = null; try { bs = system.windows.interop.imaging.createbitmapsourcefromhbitmap(ip, intptr.zero, int32rect.empty, system.windows.media.imaging.bitmapsizeoptions.fromemptyoptions()); } { deleteobject(ip); } return bs; }
setting uselayoutrounding="true"
on main window should solve issue.
Comments
Post a Comment