성태의 닷넷 이야기
홈 주인
모아 놓은 자료
프로그래밍
질문/답변
사용자 관리
사용자
메뉴
아티클
외부 아티클
유용한 코드
온라인 기능
MathJax 입력기
최근 덧글
[정성태] Detecting blocking calls using asyn...
[정성태] 아쉽게도, 커뮤니티는 아니고 개인 블로그입니다. ^^
[정성태] 질문이 잘 이해가 안 됩니다. 우선, 해당 소스코드에서 ILis...
[양승조
] var대신 dinamic으로 선언해서 해결은 했습니다. 맞는 해...
[양승조
] 또 막혔습니다. ㅠㅠ var list = props[i].Ge...
[양승조
] 아. 감사합니다. 어제는 안됐던것 같은데....정신을 차려야겠네...
[정성태] "props[i].GetValue(props[i])" 코드에서 ...
[정성태] 저렇게 조각 코드 말고, 실제로 재현이 되는 예제 프로젝트를 압...
[정성태] Modules 창(Ctrl+Shift+U)을 띄워서, 해당 Op...
[정성태] 만드실 수 있습니다. 단지, Unity 엔진 내의 스크립트와 W...
글쓰기
제목
이름
암호
전자우편
HTML
홈페이지
유형
제니퍼 .NET
닷넷
COM 개체 관련
스크립트
VC++
VS.NET IDE
Windows
Team Foundation Server
디버깅 기술
오류 유형
개발 환경 구성
웹
기타
Linux
Java
DDK
Math
Phone
Graphics
사물인터넷
부모글 보이기/감추기
내용
<div style='display: inline'> <h1 style='font-family: Malgun Gothic, Consolas; font-size: 20pt; color: #006699; text-align: center; font-weight: bold'>C# - EXE/DLL로부터 추출한 이미지/아이콘의 배경색 투명 처리</h1> <p> 지난번에 EXE/DLL로부터 이미지 추출하는 방법을 설명했는데요.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > 프로그램에 보여지는 리소스(예: 아이콘) 추출하는 방법 ; <a target='tab' href='http://www.sysnet.pe.kr/2/0/1524'>http://www.sysnet.pe.kr/2/0/1524</a> </pre> <br /> 실제로 해보니, 아이콘은 투명 처리가 잘 되어 있는데 이미지의 경우에는 BMP 파일로 저장되어 불투명 이미지가 나왔습니다. 그래서 투명 처리가 필요했는데요. 그래픽 툴을 사용하는 것보다 명령행 도구가 없나 해서 검색해 보았더니 다음의 도구가 나오는군요. ^^<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > ImageMagick - Convert, Edit, and Compose Images ; <a target='tab' href='http://www.imagemagick.org'>http://www.imagemagick.org</a> Windows Binary Release ; <a target='tab' href='http://www.imagemagick.org/script/binary-releases.php#windows'>http://www.imagemagick.org/script/binary-releases.php#windows</a> </pre> <br /> <a target='tab' href='http://www.imagemagick.org/download/binaries/ImageMagick-6.8.7-4-Q16-x86-windows.zip'>압축 버전</a>도 제공하므로 이를 내려받아 해제합니다. exe 파일이 몇 개 나오는데, 그중에서 convert.exe 실행 파일을 이용하면 됩니다.<br /> <br /> 예를 들어 BMP 이미지에서 0xFF00FF RGB 색을 투명으로 처리하고 싶다면 이렇게 실행해 줍니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > convert file.bmp -transparent #ff00ff file.png </pre> <br /> 그런데, EXE/DLL로부터 추출된 이미지 같은 경우에는 가끔 다음과 같은 식으로 오류가 발생하면서 처리가 안 될 때가 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > C:temp>convert file.bmp -transparent #ff00ff file.png convert.exe: Length and filesize do not match `file.bmp' @ error/bmp.c/ReadBMPImage/817. </pre> <br /> 혹시나 싶어, convert.exe 폴더에 있는 다른 실행 파일인 imdisplay.exe로 파일을 열어도 역시 마찬가지의 오류가 발생합니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > IMDisplayDoc function [DoReadImage] reported an error. IMDisplay.exe: length and filesize do not match '...' @ error/bmp.c/ReadBMPImage/817 </pre> <br /> 가만보니, palette가 구성된 이미지인 경우인데 Visual Studio로 확인하면 다음과 같이 왼쪽에 팔렛트가 펼쳐집니다.<br /> <br /> <img alt='bmp2png_transparent_1.png' src='/SysWebRes/bbs/bmp2png_transparent_1.png' /> <br /><br /> 이런 BMP 파일은 그냥 그림판(mspaint.exe)으로 열어서 24bit BMP 파일로 저장해 주고 convert 프로그램을 실행하면 됩니다.<br /> <br /> <hr style='width: 50%' /><br /> <br /> ImageMagick의 convert.exe에는 많은 옵션이 있지만 투명 처리 정도는 C#으로도 간단히 할 수 있습니다. 다음은 이에 대한 코드를 담고 있는 글입니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Setting transparency in an image ; <a target='tab' href='http://blogs.msdn.com/b/jmstall/archive/2007/08/06/setting-transparency-in-an-image.aspx'>http://blogs.msdn.com/b/jmstall/archive/2007/08/06/setting-transparency-in-an-image.aspx</a> </pre> <br /> 나중에 찾아 보기 쉽도록 여기다 코드를 그대로 복사해 두죠. ^^<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > // Simple tool to mark a color on the bitmap transparent. // http://blogs.msdn.com/jmstall using System; using System.Collections.Generic; using System.Text; using System.Drawing; using System.IO; namespace MakeTransparent { class Program { static void Main(string[] args) { if (args.Length == 0) { Console.WriteLine( @"Usage: MakeTransparent <filename> Makes the background color in the image transparent. This assumes the pixel at 0,0 is the background color. This saves it as a new file appended with '.t', so that it doesn't erase the original filename. For example 'c:\dir\a.bmp' becomes 'c:\dir\a.t.bmp'. Callers can rename as needed. "); return; } string filename = args[0]; filename = Path.GetFullPath(filename); Console.WriteLine("Loading image from:{0}", filename); Bitmap myBitmap = new Bitmap(filename); // Get the color of a background pixel. // Assume upper left corner is opaque. Color backColor = myBitmap.GetPixel(0, 0); Console.WriteLine("Choosing background color based of pixel at (0,0). Color ={0}", backColor.ToString()); // Make backColor transparent for myBitmap. This is the heart of the program. myBitmap.MakeTransparent(backColor); // Change "c:\dir\thing.bmp" to "c:\dir\thing.t.bmp" string left = Path.ChangeExtension(filename, null); string ext = Path.GetExtension(filename); // includes period ".bmp" string outFile = left + ".t" + ext; Console.WriteLine("Saving back to file: {0}", outFile); myBitmap.Save(outFile); } } } </pre> <br /> 주석에도 달려 있지만, (0,0) 위치의 색을 투명색으로 지정할 뿐 별다르게 특별한 처리는 없습니다.<br /> <br /> <hr style='width: 50%' /><br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > // <a target='tab' href='https://twitter.com/PR0GRAMMERHUM0R/status/1773122861487575208/photo/1'>https://twitter.com/PR0GRAMMERHUM0R/status/1773122861487575208/photo/1</a> from rembg import remove from PIL import Image input = Image.open('cl.jpg') output = remove(input) output.save('output.png') </pre> </p><br /> <br /><hr /><span style='color: Maroon'>[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]</span> </div>
첨부파일
스팸 방지용 인증 번호
3085
(왼쪽의 숫자를 입력해야 합니다.)