투명한 이미지 그리기
-
05-07-2019 - |
문제
C#에 CSS 스프라이트 엔진을 작성하고 있지만 몇 가지 문제가 있습니다. 마스터 이미지를 만들고 모든 속성을 설정 한 다음 반복합니다. sprites
마스터 이미지에 그려 듭니다. 그러나 마스터 이미지를 저장하기 위해 오면 투명한 배경을 가진 빈 마스터 이미지 일뿐입니다. sprites
. 나는 내가 어디에서 잘못 될지 매우 혼란스러워한다.
내가 사용하는 코드는 다음과 같습니다.
// Work out the width/height required
int max_width = 0;
int max_height = 0;
foreach(SpriteInformation sprite in sprites) {
if (max_width < (sprite.Left + greatest_width)) max_width = sprite.Left + greatest_width;
if (max_height < (sprite.Top + greatest_height)) max_height = sprite.Top + greatest_height;
}
// Create new master bitmap
Bitmap bitmap = new Bitmap(max_width,max_height,PixelFormat.Format32bppArgb);
Graphics graphics = Graphics.FromImage(bitmap);
// Set background color
SolidBrush brush;
if (cbxBackground.Checked) {
if (txtColor.Text == "") {
brush = new SolidBrush(Color.Black);
} else {
brush = new SolidBrush(pnlColor.BackColor);
}
} else {
if (txtColor.Text == "") {
brush = new SolidBrush(Color.White);
} else {
brush = new SolidBrush(pnlColor.BackColor);
}
}
//graphics.FillRectangle(brush,0,0,bitmap.Width,bitmap.Height);
bitmap.MakeTransparent(brush.Color);
graphics.Clear(brush.Color);
// Copy images into place
ImageAttributes attr = new ImageAttributes();
//attr.SetColorKey(brush.Color,brush.Color);
foreach(SpriteInformation sprite in sprites) {
Rectangle source = new Rectangle(0,0,sprite.Width,sprite.Height);
Rectangle dest = new Rectangle(sprite.Left,sprite.Top,sprite.Width,sprite.Height);
graphics.DrawImage(sprite.Sprite,dest,0,0,sprite.Width,sprite.Height,GraphicsUnit.Pixel,attr);
}
// Save image
string format = ddlFormat.Items[ddlFormat.SelectedIndex].ToString();
if (format == "PNG") {
dlgSave.Filter = "PNG Images|*.png|All Files|*.*";
dlgSave.DefaultExt = ",png";
if (dlgSave.ShowDialog() == DialogResult.OK) {
bitmap.Save(dlgSave.FileName,ImageFormat.Png);
}
} else if (format == "JPEG") {
} else {
}
해결책
Sprite.left와 Top은 무엇입니까? 그들이 0이 아니라면 그것은 문제 일 수 있습니다. 나는 당신이 목적지와 소스가 잘못된 길을 가지고 있다고 생각합니까?
http://msdn.microsoft.com/en-us/library/ms142045.aspx
아직하지 않은 경우 더 간단한 DrawImage 변형을 시도하십시오. 예를 들어 DrawImage(sprite.Sprite,0,0)
다른 팁
"그래픽"으로 그리지 만 "비트 맵"을 저장합니까? 해당 그래픽이 원래 "비트 맵"객체와 내부적으로 작동하는지 확실하지 않습니다 ...
제휴하지 않습니다 StackOverflow