我正在嘗試爲我的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我已經添加了一些代碼:) –