2017-08-12 130 views
0

我正在尝试为我的monogame项目创建一个使用Windows窗体的关卡编辑器,并且需要将基于小像素的图像绘制到没有质量损失的图片框时进行缩放。在monogame中,当我需要这样做时,我可以将绘图类型设置为PointClamp,然后每个像素按原样绘制,而不是在缩放时进行像素化;我希望通过一个picturebox来做这样的事情。现在它看起来像this但我更喜欢像this更清晰干净的图像(第二个是它将出现在monogame中)。我没有上传任何代码,但假设我从文件流中抓取了一个图像,并使用位图构造函数来扩展它(不要认为这是相关的,但我会把它放在那里)。CSharp Windows Form Picturebox绘制没有质量损失的小图像

Image croppedImage, image = tileMap.tileBox.Image; 
var brush = new SolidBrush(Color.Black); 

try { croppedImage = CropImage(image, tileMap.highlightedRect); } catch { 
    return; // If crop target is outside bounds of image then return 
} 

float scale = Math.Min(higlightedTileBox.Width/croppedImage.Width, higlightedTileBox.Height/image.Height); 

var scaleWidth = (int)(higlightedTileBox.Width * scale); 
var scaleHeight = (int)(higlightedTileBox.Height * scale); 

try { higlightedTileBox.Image = new Bitmap(croppedImage, new Size(scaleWidth, scaleHeight)); } catch { 
    return; // Image couldn't be scaled or highlighted tileBox couldn't be set to desired image 
} 

CropImage:

private static Image CropImage(Bitmap img, Rectangle cropArea) { 
    return img.Clone(cropArea, img.PixelFormat); 
} 

private static Image CropImage(Image img, Rectangle cropArea) { 
    return CropImage(new Bitmap(img), cropArea); 
} 

上面的代码是我的当前方法在它的全部内容。 tileMap是一个窗体,tilebox是该窗体中的图片框.image是在被剪裁为用户突出显示的内容之前的完整spritesheet纹理。裁剪后,我尝试将当前的图片框(突出显示的文本框)图像设置为裁剪图像的放大版本。

+0

我们需要更多的代码! – leAthlon

+1

好吧,给我一秒 –

+1

@leAthlon我已经添加了一些代码:) –

回答

1

所以我通过尝试了一下就得到了一个解决方案。 它看起来像按比例缩放图像直接使用某种插值。 要尝试Winforms支持的不同插值模式,我创建了一个小演示。您可以看到,每个标签都包含InterpolationMode的名称,后面跟着它的结果图像。我使用的原始位图是顶部的小图。 enter image description here 从您的问题看,您似乎希望实现类似NearestNeighbour的内容。

以下代码缩放bmp,结果存储在bmp2中。试试如果这就是你想要的。考虑建立一个适当的实现,如果你使用这个解决方案(处置未使用的位图等)。 我希望它有帮助。

 Bitmap bmp = new Bitmap("test.bmp"); 
     Bitmap bmp2; 
     Graphics g = Graphics.FromImage(bmp2=new Bitmap(bmp.Width * 2, bmp.Height * 2)); 
     g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor; 
     g.DrawImage(bmp, 0, 0, bmp.Width * 2, bmp.Height * 2); 
     g.Dispose(); 
+0

Thnx我会试试看。刚花了一个小时试图为我的表单创建一个XNA控件,但这看起来更容易,所以我只是删除它。 –

+1

是的,终于我得到了它的工作。 thnx这么多:) –

+0

如果它是你想要的,考虑接受答案。 – leAthlon

相关问题