Winform 에서 DrawToBitmap 에서 배경색을 투명하게 하고 싶습니다.
-
2012년 8월 16일 목요일 오후 4:01
Control 를 DrawToBitmap 으로 Bitmap 을 생성하여 로컬에 저장(Save) 하였습니다. 물론 포멧은 png 로 했습니다.
그런데 배경색이 그대로 나오더군요.
심지어는 윈폼의 배경색을 투명하게 바꾸고 똑같은 과정을 수행 하였지만 역시나 배경색이 투명이 되지 않았습니다.
윈폼에서는 불가능 한건가요 ?
모든 응답
-
2012년 8월 17일 금요일 오전 2:23
Control이 어떤 컨트롤인지는 모르겠으나 기본적으로 컨트롤의 배경색은 Transparent가 적용되지 않습니다.
(참조: http://msdn.microsoft.com/en-us/library/wk5b13s4.aspx )
picturebox를 사용자 정의 컨트롤로 만들어서 위 msdn내용처럼
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
설정을 하시고
this.BackColor = Color.Transparent;
해서 사용하시는 방법이 있습니다.
다른 방법은 배경이 투명인 별도의 Image 객체를 가지고 있다가 이것을 저장해서 사용하는 방법입니다.
아래와 같이 테스트 해보았습니다.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication26 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Image image = new Bitmap(pictureBox1.Width, pictureBox1.Height); Graphics g = Graphics.FromImage(image); g.Clear(Color.Transparent); g.DrawEllipse(new Pen(Color.Red, 10), 50, 50, 150, 150); pictureBox1.Image = image; image.Save(@"c:\temp\test2.png"); } } }실행을 하면 아래와 같이 원이 그려지고 파일로 저장이 됩니다.
배경이 정말 투명한지는 아래와 같이 테스트 해보았습니다.
<BODY style="background-color: #008"> <IMG SRC="C:/temp/TEST.png" > </BODY>
MSDN에 있는 내용은 시간이 되면 한번 해봐야 겠네요. ^^
-
2012년 8월 17일 금요일 오전 3:18네 답변 감사합니다. 제가 설명이 부족했군요, 사실 xna에서 한글폰트 지원이 부족한 이유에서 생각한것이 윈폼의 Label 컨트롤을 이용하여 텍스트를 입력한후 그것을 drawtobitmap으로 찍어 텍스쳐로 활용하자는 취지 였습니다. 결과 아주 잘 작동합니다만 배경색이 빠지지 않아 쉐이더를 사용하여 키 값을 빼곤 했는데 혹시나 원초적으로 배경을 제거하는 방법이 있지 않을까 해서 질문하였습니다. 정리 하자면 Label 컨트롤의 텍스트를 DrawToBitmap 으로 저장시 배경색을 투명으로 만드는 방법이 있나요?
-
2012년 8월 17일 금요일 오전 5:14
DrawToBitmap 으로는 잘 안되는군요.
조금 응용해서 다음과 같이 해보세요. 투명한 이미지에 DrawString을 이용해서 Label에 있는 그대로 그렸습니다.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Drawing.Drawing2D; namespace WindowsFormsApplication29 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { Image img = new Bitmap(label1.Width, label1.Height); Graphics g = Graphics.FromImage(img); g.Clear(Color.Transparent); g.DrawString(label1.Text, label1.Font, new SolidBrush(label1.ForeColor), new Point(0, 0)); img.Save(@"c:\temp\test.png"); } } }투명한지 테스트
- 답변으로 표시됨 Jina LeeModerator 2012년 9월 3일 월요일 오전 7:53

