wxWidgets 을 이용해서 만들었던 간단한 이미지 체크툴을 C# WPF 으로 만들어 보기 위해서 구글링을 통해 알아본 해당 코드들... 음 이미지 동일 여부 판단 하는 부분은 빠져 있지만 다른 잡다한 코드가 많이 있던 wxWidgets + C/C++ 보다는.. 간결한 코드가 나온다 ;ㅂ; (.NET 3.5 가 필요하지..) 농담 아니고 정말-ㅅ- 간단하게 처리된다!;;


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;

namespace WpfEqMask
{
    /// 
    /// Window1.xaml에 대한 상호 작용 논리
    /// 
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        // 이미지 처리방법 관련 (외국) 글.
        // http://10rem.net/blog/2010/03/24/rotating-and-cropping-an-image-in-wpf

        // http://stackoverflow.com/questions/4402828/wpf-cut-and-save-image
        public void MakeRectImage(BitmapImage srcBitmap, int x, int y, int width, int height, string filename)
        {
            TransformGroup transformGroup = new TransformGroup();
            /*
            RotateTransform rotateTransform = new RotateTransform(angle);
            rotateTransform.CenterX = sourceImage.PixelWidth / 2.0;
            rotateTransform.CenterY = sourceImage.PixelHeight / 2.0;
            transformGroup.Children.Add(rotateTransform);
            */
            TranslateTransform translateTransform = new TranslateTransform();
            translateTransform.X = -x;
            translateTransform.Y = -y;
            transformGroup.Children.Add(translateTransform);

            DrawingVisual vis = new DrawingVisual();
            DrawingContext cont = vis.RenderOpen();
            cont.PushTransform(transformGroup);
            cont.DrawImage(srcBitmap, new Rect(new Size(srcBitmap.PixelWidth, srcBitmap.PixelHeight)));
            cont.Close();

            RenderTargetBitmap rtb = new RenderTargetBitmap(width, height, 96d, 96d, PixelFormats.Default);
            rtb.Render(vis);

            FileStream stream = new FileStream(filename, FileMode.Create);
            
            PngBitmapEncoder encoder = new PngBitmapEncoder(); // png 포멧으로 저장.
            encoder.Frames.Add(BitmapFrame.Create(rtb));
            encoder.Save(stream);
            stream.Close();
        }

        private void OnDrop(object sender, DragEventArgs e)
        {
            string[] dropFileList = e.Data.GetData(DataFormats.FileDrop, true) as string[];

            string result = result = "Drop File List\n";
            foreach (string file in dropFileList)
            {
                result += file;
                result += "\n";

                string[] bmpFiles = Directory.GetFiles(file, "*.bmp", SearchOption.AllDirectories);
                if (bmpFiles.Length > 0)
                {
                    result += "---------------------\n";
                    result += "폴더내 bmp 목록.\n";

                    foreach (string bmpFile in bmpFiles)
                    {
                        // MyCanvas 에 추가~
                        Image img = new Image();
                        BitmapImage src = new BitmapImage();
                        src.BeginInit();
                        src.UriSource = new System.Uri(bmpFile, System.UriKind.Relative);
                        src.EndInit();
                        img.Source = src;
                        img.Stretch = Stretch.Uniform;

                        try
                        {
                            string rectname = bmpFile;
                            rectname += ".rect.bmp";
                            MakeRectImage(src, 10, 10, 32, 32, rectname);
                            
                            img.Width = src.Width;
                            img.Height = src.Height;
                            MyCanvas.Children.Add(img);
                        }
                        catch (Exception ec)
                        {
                            MessageBox.Show("예외발생" + ec.ToString());
                        }

                        result += bmpFile;
                        result += "\n";

                        MessageBox.Show("로딩 잘되네");
                    }
                }
            }

            MessageBox.Show(result, "결과");
        }
    }
}


Posted by ngcbbs
,