我正在尝试为我的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纹理。裁剪后,我尝试将当前的图片框(突出显示的文本框)图像设置为裁剪图像的放大版本。
我们需要更多的代码! – leAthlon
好吧,给我一秒 –
@leAthlon我已经添加了一些代码:) –