private void ResizeWithImageSharp(string filePath, string resizedFilePath, int size) { using (SixLabors.ImageSharp.Image <Rgba32> image = SixLabors.ImageSharp.Image.Load(filePath)) { image.Mutate(ctx => ctx.Resize(size, size)); image.Save(resizedFilePath); } }
public static System.Drawing.Bitmap ToBitmap <TPixel>(this SixLabors.ImageSharp.Image <TPixel> image) where TPixel : unmanaged, IPixel <TPixel> { using (var memoryStream = new MemoryStream()) { var imageEncoder = image.GetConfiguration().ImageFormatsManager.FindEncoder(PngFormat.Instance); image.Save(memoryStream, imageEncoder); memoryStream.Seek(0, SeekOrigin.Begin); return(new System.Drawing.Bitmap(memoryStream)); } }
public byte[] GetVideoImageByte(int imageId, int?maxWidth) { var imageInfo = this._imageRepo.Get(imageId); if (imageInfo != null) { var imageRootPath = this._settingService.GetSettingValue(SettingKey.ImageDir); var imageFullPath = imageRootPath + imageInfo.Path.Replace("/", "\\"); var imageExt = Path.GetExtension(imageInfo.Path); if (System.IO.File.Exists(imageFullPath)) { // 原图输出 if (!maxWidth.HasValue) { return(System.IO.File.ReadAllBytes(imageFullPath)); } else { // 处理缩略图 using (SixLabors.ImageSharp.Image <Rgba32> srcImg = SixLabors.ImageSharp.Image.Load(imageFullPath)) { int resetWidth = maxWidth.Value; if (resetWidth > srcImg.Width) { resetWidth = srcImg.Width; } // 缩细倍数 var imgP = (Convert.ToDouble(resetWidth) / Convert.ToDouble(srcImg.Width)); var resetHeight = Convert.ToInt32(Convert.ToDouble(srcImg.Height) * imgP); srcImg.Mutate(x => x.Resize(resetWidth, resetHeight)); using (System.IO.MemoryStream ms = new MemoryStream()) { srcImg.Save(ms, SixLabors.ImageSharp.ImageFormats.Jpeg); byte[] imageByte = ms.ToArray(); return(imageByte); } } } } } return(null); }
/// <summary> /// 获取图片二进制数据 /// </summary> /// <param name="path">物理路径</param> /// <param name="maxWidth">最大宽度</param> /// <returns></returns> protected byte[] GetImageByte(string path, int?maxWidth) { if (System.IO.File.Exists(path)) { var imageFullPath = path.Replace("/", "\\"); var imageExt = Path.GetExtension(imageFullPath); // 原图输出 if (!maxWidth.HasValue) { return(System.IO.File.ReadAllBytes(imageFullPath)); } else { // 处理缩略图 using (SixLabors.ImageSharp.Image <Rgba32> srcImg = SixLabors.ImageSharp.Image.Load(imageFullPath)) { int resetWidth = maxWidth.Value; if (resetWidth > srcImg.Width) { resetWidth = srcImg.Width; } // 缩细倍数 var imgP = (Convert.ToDouble(resetWidth) / Convert.ToDouble(srcImg.Width)); var resetHeight = Convert.ToInt32(Convert.ToDouble(srcImg.Height) * imgP); srcImg.Mutate(x => x.Resize(resetWidth, resetHeight)); using (System.IO.MemoryStream ms = new MemoryStream()) { srcImg.Save(ms, SixLabors.ImageSharp.ImageFormats.Jpeg); byte[] imageByte = ms.ToArray(); return(imageByte); } } } } return(null); }