/// <summary> /// Generate the thumb image /// </summary> /// <param name="image">the image created</param> /// <param name="maxWidthHeight">image height limit</ /// <param name="fileWidth">the original image width</param> /// <param name="fileHeight">the original image height</param> /// <param name="fullFilePath">the image path</param> /// <param name="ext">the image extention</param> /// <param name="thumbnailFileName">thumbnail file name</param> protected void GenerateThumbnail(System.Drawing.Image image, int maxWidthHeight, int fileWidth, int fileHeight, string fullFilePath, string ext, string thumbnailFileName) { // Generate thumbnail float fx = (float)fileWidth / (float)maxWidthHeight; float fy = (float)fileHeight / (float)maxWidthHeight; // must fit in thumbnail size float f = Math.Max(fx, fy); //if (f < 1) f = 1; int widthTh = (int)Math.Round((float)fileWidth / f); int heightTh = (int)Math.Round((float)fileHeight / f);
// fixes for empty width or height if (widthTh == 0) widthTh = 1; if (heightTh == 0) heightTh = 1;
// Create new image with best quality settings Bitmap bp = new Bitmap(widthTh, heightTh); Graphics g = Graphics.FromImage(bp); g.SmoothingMode = SmoothingMode.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.PixelOffsetMode = PixelOffsetMode.HighQuality;
// Copy the old image to the new and resized Rectangle rect = new Rectangle(0, 0, widthTh, heightTh); g.DrawImage(image, rect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);
// Copy metadata ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo codec = null; for (int i = 0; i < codecs.Length; i++) { if (codecs[i].MimeType.Equals("image/jpeg")) codec = codecs[i]; }
// Set compresion ratio to 90% EncoderParameters ep = new EncoderParameters(); ep.Param[0] = new EncoderParameter(Encoder.Quality, 90L);
// Save the new image bp.Save(thumbnailFileName, codec, ep); bp.Dispose(); g.Dispose(); }
Dim name As String = "ImageName" Dim u As New Umbraco.BusinessLogic.User(0)
Dim m = Umbraco.cms.businesslogic.media.Media.MakeNew(name, Umbraco.cms.businesslogic.media.MediaType.GetByAlias("Alias"), u, ParentID)
'--Save the file to disk
Dim pf As New Umbraco.cms.businesslogic.media.PostedMediaFile()
pf.FileName = name
pf.ContentLength = str.Length
pf.DisplayName = name
pf.InputStream = str
pf.ReplaceExisting = True
Dim factory As New Umbraco.cms.businesslogic.media.UmbracoImageMediaFactory()
factory.DoHandleMedia(m, pf, u)
'--Add other properties
m.getProperty("originalUrl").Value = img.Url
m.getProperty("source").Value = img.Source
m.Save()
str is the inputstream from the fileupload control
creating a new media item
I am using a file upload control to upload an image to the server, how can I save this image in the site backoffice in the appropriate media folder?
this is the file upload code behind:
anybody can help with this? I am missing just the part of integrating the image in to my backoffice
Found a hint in another post this is my code:
Does anyone knows how to create a thumbnail image? I didn't understand the instructions from the post.
Found the solution, it's a bit long but it does the work for me :)
or you could let Umbraco do everything for you:
is working on a reply...