コード例 #1
0
        public void Convert(string source, string destination, string type)
        {
            System.Drawing.Image image = System.Drawing.Image.FromFile(source);

            StrategyContext context;

            switch (type.ToLower())
            {
            case "jpeg":
            case "jpg":
                context = new StrategyContext(new ConvertToJpeg());
                break;

            case "gif":
                context = new StrategyContext(new ConvertToGif());
                break;

            case "png":
                context = new StrategyContext(new ConvertToPng());
                break;

            default:
                throw new Exception("Wrong file format chosen!");
            }

            context.ConvertImage(image, destination);
        }
コード例 #2
0
        public void Convert(string source, string destination, string type)
        {
            //byte[] img_bytes = File.ReadAllBytes(source); //read img file into memory
            //MemoryStream img_ms = new MemoryStream(img_bytes);
            //Image img = Image.FromStream(img_ms);


            //MemoryStream save_ms = new MemoryStream();
            //((Bitmap)img).Save(save_ms, ImageFormat.Jpeg);
            //byte[] resaved_img = save_ms.ToByteArray();

            //create object from source file
            Image image = Image.FromFile(source);

            //implement context strategy from context using IMG object instead of source...
            StrategyContext context;

            switch (type.ToLower())
            {
            case "jpeg":
            case "jpg":
                context = new StrategyContext(new ConvertToJpeg());
                break;

            case "gif":
                context = new StrategyContext(new ConvertToGif());
                break;

            case "png":
                context = new StrategyContext(new ConvertToPng());
                break;

            default:
                throw new Exception("Wrong file format chosen!");
            }

            context.ConvertImage(image, destination);
        }