winforms图形与最近邻居过滤图像的裁剪边缘 [英] winforms graphics with nearest neighbor filtering clipping edges of image

查看:162
本文介绍了winforms图形与最近邻居过滤图像的裁剪边缘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个Chip8仿真器/解释器。这很好。到了这一点,我需要一个用户界面来玩一些游戏,并观看这个不好的杰克逊运动。我首先遇到了一个问题,即在屏幕上绘制framebuffer(只是一个位图)是超级模糊的。在弄清楚我需要将插值模式设置为最近邻居后,它看起来好多了,但看起来图像的边缘或者被剪切掉了,或者我误解了工作中的插值。这里有一个截图。





您在顶部,左侧和右侧看到的边缘应该以整个白色像素结尾,并与您看到的图案保持一致。特别容易看到左上角的问题,应该是一个完整的白色像素。



正在放大的图像是64x32,并且使用普通图形对象绘制,因此:(忽略64 * 6,我知道它很有趣)

  g.DrawImage(cpu.raster,0,0 ,64 * 6,32 * 6); 

其中cpu.raster是模拟器渲染游戏的位图。我会发布其余的代码,但这一切似乎都很标准。唯一与图形相关的其他代码是插值/平滑模式,它们被设置为:

  g = this .CreateGraphics(); 
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
this.DoubleBuffered = true;

是否有另一个设置我应该注意防止出现这种情况?我试图缩小绘制的图像的大小,想想也许这个表单会以某种方式裁剪它,但它不是,图像结束于您在屏幕截图中看到的边缘。

解决方案

在设置 InterpolationMode SmoothingMode 的代码中,添加此行:

  g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half; 

这将修复您正在绘制的内容的左上角。至于右侧,请确保您的表格足够宽以容纳图片。您指定的宽度为64 * 6 = 384,但在屏幕截图中,您的表单显示区域为377像素。


I'm writing a Chip8 emulator/interpreter. It's going great. To the point I need a UI to play some games and watch this bad Jackson in motion. I first ran into a problem where drawing the framebuffer (which is just a bitmap) to the screen was super-blurry. After figuring out I needed to set the interpolation mode to Nearest Neighbor it looked much better, but it seems the edges of the image are either being clipped or I'm misunderstanding the interpolation at work. Here's a screenshot.

The edges you see around the top, left, and right should end with "whole" white pixels keeping with the pattern you see. Especially easy to see the issue at the top left, should be one whole white pixel.

The image being upscaled is 64x32, and is being drawn using a normal graphics object, as such: (ignore the 64 * 6, I know it's funny)

g.DrawImage (cpu.raster, 0, 0, 64 * 6, 32 * 6);

Where "cpu.raster" is the bitmap the emulator renders the game to. I'd post the rest of the code, but it all seems pretty standard. The only other code that even relates to the graphics is the interpolation/smoothing modes, which are set as such:

        g = this.CreateGraphics ();
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
        this.DoubleBuffered = true;

Is there another setting I should be aware of to prevent this? I tried reducing the size of the drawn image, thinking maybe the form was clipping it somehow, but it's not, the image ends at the edges you see in the screenshot.

解决方案

In the code where you are setting InterpolationMode and SmoothingMode, add this line:

g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;

That will fix the top and left sides of what you are drawing. As for the right side, make sure your form is wide enough to accomodate the image. The width you specify is 64 * 6 = 384, but in the screenshot it appears your form has 377 pixels of display area.

这篇关于winforms图形与最近邻居过滤图像的裁剪边缘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆