Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // enjoy. AK :)
- using System;
- using System.Activities;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Net;
- using System.ComponentModel;
- namespace WebHelpers.DownloadImageFromUrl
- {
- public class DownloadAndSaveImage : CodeActivity
- {
- [RequiredArgument, Description("Full URL path to the image")]
- [Category("URL")]
- public InArgument<String> ImageUrl { get; set; }
- [RequiredArgument, Description("Folder to save the file in")]
- [Category("Save as...")]
- public InArgument<String> SaveFolderPath { get; set; }
- [RequiredArgument, Description("File name that the image will be saved as. Without extension!")]
- [Category("Save as...")]
- public InArgument<String> SaveFileName { get; set; }
- [RequiredArgument, Description("Image type to save as. Will determine file extension.")]
- [Category("Save as...")]
- public InArgument<ImageFormat> ImageFormatToUse { get; set; }
- [Category("Save as...")]
- public InArgument<bool> ShouldOverwrite { get; set; }
- protected override void Execute(CodeActivityContext context)
- {
- string fullSavePath = Path.Combine(SaveFolderPath.Get(context), SaveFileName.Get(context)) + "." + ImageFormatToUse.Get(context).ToString().ToLower();
- if (!ShouldOverwrite.Get(context))
- if (File.Exists(fullSavePath))
- throw new IOException("File " + fullSavePath + " already exists.");
- using (WebClient webClient = new WebClient())
- {
- byte[] data = webClient.DownloadData(ImageUrl.Get(context));
- using (MemoryStream mem = new MemoryStream(data))
- {
- var downloadedImage = Image.FromStream(mem);
- downloadedImage.Save(fullSavePath, ImageFormatToUse.Get(context));
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment