/// <summary> /// Reference this in HTML as <img src="/Photo/WatermarkedImage/{ID}" /> /// Simplistic example supporting only jpeg images. /// </summary> /// <param name="ID">Photo ID</param> public ActionResult WatermarkedImage(int id) { // Attempt to fetch the photo record from the database using Entity Framework 4.2. var file = FileManagerRepository.GetSingle(id); if (file != null) // Found the indicated photo record. { var dic = new Dictionary <String, String>(); // Create WebImage from photo data. // Should have 'using System.Web.Helpers' but just to make it clear... String url = String.Format("https://docs.google.com/uc?id={0}", file.GoogleImageId); byte[] imageData = GeneralHelper.GetImageFromUrlFromCache(url, dic); var wi = new System.Web.Helpers.WebImage(imageData); // Apply the watermark. wi.AddTextWatermark("EMIN YUCE"); // Extract byte array. var image = wi.GetBytes("image/jpeg"); // Return byte array as jpeg. return(File(image, "image/jpeg")); } else // Did not find a record with passed ID. { return(null); // 'Missing image' icon will display on browser. } }
public static byte[] AddWaterMark(string filePath, string text) { using (var img = System.Drawing.Image.FromFile(filePath)) { using (var memStream = new MemoryStream()) { using (var bitmap = new Bitmap(img)) // to avoid GDI+ errors { bitmap.Save(memStream, ImageFormat.Png); var content = memStream.ToArray(); var webImage = new WebImage(memStream); webImage.AddTextWatermark(text, verticalAlign: "Top", horizontalAlign: "Left", fontColor: "Brown"); return webImage.GetBytes(); } } } }
public ActionResult ShowGuidWithWatermark(string guid, string path, string width, string height) { int intWidth = 0; int intHeight = 0; if (!int.TryParse(width, out intWidth)) { return null; } if (!int.TryParse(height, out intHeight)) { return null; } if (string.IsNullOrWhiteSpace(guid) || string.IsNullOrWhiteSpace(path)) { return null; } string photoName = ""; string coordinate = ""; var dc = new DbWithBasicFunction(); var db = dc.db; if (path.ToLower() == "gallery") { var galleryItem = db.tbl_gallery.Where(a => a.guid == guid).FirstOrDefault(); if (galleryItem == null) { return null; } photoName = galleryItem.photo; coordinate = galleryItem.photoCoordinate; } var item = getFileCoordinated(photoName, coordinate, path); if (item != null) { var settings = new ResizeSettings(); int widthInt = int.Parse(width); int heightInt = int.Parse(height); settings.Width = widthInt; settings.Height = heightInt; settings.Mode = FitMode.Stretch; WebImage waterMarkImage; using (MemoryStream st = new MemoryStream()) { ImageBuilder.Current.Build(item.First().Key, st, settings); st.Position = 0; waterMarkImage = new WebImage(st); } waterMarkImage.AddTextWatermark("www.titizkromaksesuar.com", fontColor: "#ffe27b", fontSize: 10, verticalAlign: "Middle", opacity: 60); return File(waterMarkImage.GetBytes(), item.First().Value); } else { return null; } }
private Byte[] WaterMark(Stream s,string color) { MemoryStream str = new MemoryStream(); System.Web.Helpers.WebImage webimg = new System.Web.Helpers.WebImage(s); String wm = WebSecurity.CurrentUserName; webimg.AddTextWatermark(wm, color, 16, "Regular", "Microsoft Sans Serif", "Right", "Bottom", 50, 10); return webimg.GetBytes(); }
/// <summary> /// Resizes an image and saves it to disk. /// </summary> /// <param name="webImage">Web image object</param> /// <param name="image">The image object</param> private static string ResizeImageAndSaveToDisk(WebImage webImage) { webImage = webImage.AddTextWatermark(".NET Jumpstart"); // Resize image with web image helper webImage = webImage.Resize(1024, 768, true, true); // Create random name var guid = Guid.NewGuid(); var extension = Path.GetExtension(webImage.FileName); // Set filename and save to disk string fileName = string.Format("{0}{1}", guid, extension); var path = GetLocalFilePath(fileName); webImage.Save(path); return fileName; }
public ActionResult GetResource(Guid id, int? width, int? height, bool? noSource) { //get the resource. var res = db.Resources.SingleOrDefault(r => r.ID == id); if (res == null) return HttpNotFound("That resource cannot be found."); var path = Path.Combine(Server.MapPath("~/ResourceUploads"), id.ToString()); var stream = new FileStream(path, FileMode.Open); if (stream.Length == 0) throw new HttpException(503, "An internal server error occured whilst fetching the resource."); Stream outputStream = stream; if (res.Type.StartsWith("image")) { var image = new WebImage(stream); if (width.HasValue && height.HasValue) { //Resize! image.Resize(width.Value, height.Value, true, true); } if (!string.IsNullOrWhiteSpace(res.Source) && noSource != true) { image.AddTextWatermark(res.Source, "#" + ((res.SourceTextColor == null) ? "FFFFFF" : res.SourceTextColor.Value)); } outputStream = new MemoryStream(image.GetBytes()); } stream.Close(); return File(outputStream, res.Type); }