Closest bitmap causes tearing

I created a test application that generates a Code 39 barcode. The problem is that when I display the barcode on the screen, it either gets blurry or tears. If I use BitmapScalingMode other than NearestNeighbor , I get a blurry barcode. When I use NearestNeighbor , I get a diagonal slash as shown below. Diagonal occurs only when the window is resized. (It stays there if I stop at the right place.) The image itself does not resize, but instead moves around the screen when the window is resized.

I tried using RenderOptions.EdgeMode="Aliased" , but it does not seem to have any effect ...

Gap / Blur / Correction

Torn barcodeBlurred barcodeCorrect barcode

WPF Code Example:

 <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(); 

Code 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 the barcode: addBar(gfx, black, white, '*'); foreach (char c in barcode) { addCharacter(gfx, black, white, c); } // End the barcode: addBar(gfx, black, white, '*'); } 

An example of adding a rectangle:

 g.FillRectangle(white, left, top, narrow, height); left += narrow; 

Loading a bitmap from another 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()); } finally { DeleteObject(ip); } return bs; } 
+6
source share
1 answer

Setting UseLayoutRounding="True" in the main window should solve this problem.

+2
source

All Articles