본문 바로가기

Technique/C#

System.Drawing.Graphics 를 이용한 이미지 편집

반응형

프로젝트를 개발하다 이미지를 캡쳐후에 캡쳐된 이미지에 캡쳐한 날짜와 하단에 로고를 삽입해야할 일이 생겨 

해당 모듈을 만들게 되었습니다.


혹시나 저같이 필요하신 분이은 얼마든지 가져다 사용하셔도 됩니다.


 

 

 

 

 private void textImage(string imageFile)

        {


            try {

                System.Drawing.Bitmap objBitmap = new System.Drawing.Bitmap(imageFile);

                var objBlackBrush = new System.Drawing.SolidBrush(System.Drawing.Color.FromName("white")); //날짜 색을 지정하기 위해


                string strDate = DateTime.Now.ToString("yyyy.MM.dd");  //현제 날짜 확인


                System.Drawing.Image thumbnail = null;

                System.Drawing.Image i = null;

                RectangleF logoRect = new RectangleF(0.0f, 0.0F, 0.0f, 0.0f);

                if (Properties.Settings.Default.bottomLogo == true)  //하단 로고를 삽입 할꺼냐 말꺼냐 체크

                {

                    i = System.Drawing.Image.FromFile(Properties.Settings.Default.bottomLogoPath); //하단 로고 이미지 불러오기

                    logoRect = new RectangleF(0.0f, objBitmap.Height, objBitmap.Width, i.Height); //로고의 크기 및 그려질 위치 지정


                    thumbnail = new Bitmap(objBitmap.Width, objBitmap.Height + i.Height); //thumbnail에 로고를 해당위치에 그리기


                }

                else

                {     // 로고 삽입이 없을때 실행

                    thumbnail = new Bitmap(objBitmap.Width, objBitmap.Height);  

                }



                System.Drawing.Graphics objGraphics = System.Drawing.Graphics.FromImage(thumbnail); 


                objGraphics.DrawImage(objBitmap, new RectangleF(0.0f, 0.0F, objBitmap.Width, objBitmap.Height)); //캡쳐한 이미지를 그림


                objGraphics.DrawString(strDate, new System.Drawing.Font("고딕", 25, System.Drawing.FontStyle.Bold), objBlackBrush, 1680, objBitmap.Height - 50); //날짜 출력

                if (Properties.Settings.Default.bottomLogo == true)

                {

                    objGraphics.DrawImage(i, logoRect); //하단 로고 삽입

                }




                System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;


                myEncoderParameters = new EncoderParameters(1);

                myEncoderParameter = new System.Drawing.Imaging.EncoderParameter(myEncoder, 100L);

                myEncoderParameters.Param[0] = myEncoderParameter;               

                


                myImageCodecInfo = GetEncoderInfo("image/png");

                var newFilename = Properties.Settings.Default.tempPath+"\\" + DateTime.Now.ToString("yyyyMMdd") + "\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + "_Print.png";

                thumbnail.Save(newFilename, myImageCodecInfo, myEncoderParameters);            

                mainWindow.savePhotoName = newFilename;

            

                objBitmap.Dispose();

            }catch(Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

        }

 

 

 

 


반응형