private static void ConvertWebPToImage(string path, ISupportedImageFormat format, string formatString) { byte[] b_image; try { b_image = File.ReadAllBytes(path); } catch (Exception e) { MessageBox.Show("读入图片时出现错误:\n" + e.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error); return; } using (MemoryStream inStream = new MemoryStream(b_image)) { using (ImageFactory imageFactory = new ImageFactory(preserveExifData: true)) { string savePath = Path.GetDirectoryName(path) + @"/" + Path.GetFileNameWithoutExtension(path) + formatString; try { WebPFormat webpFormat = new WebPFormat(); imageFactory.Load(webpFormat.Load(inStream)).Format(format).Save(savePath); } catch (Exception e) { MessageBox.Show("转换时出现错误:\n" + e.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error); return; } } } }
/// <inheritdoc /> public override async Task InvokeAsync(ILoadingContext <ImageSource> context, LoadingPipeDelegate <ImageSource> next, CancellationToken cancellationToken = default) { if (context.Current is Stream stream) { if (!stream.CanSeek) { var memoryStream = new MemoryStream(); await stream.CopyToAsync(memoryStream); memoryStream.Seek(0, SeekOrigin.Begin); stream = memoryStream; } var isWebP = IsWebP(stream); var tcs = new TaskCompletionSource <ImageSource>(); await Task.Run(() => { Image webPImage = null; if (isWebP) { var webPFormat = new WebPFormat(); webPImage = webPFormat.Load(stream); } if (webPImage != null) { var webPMemoryStream = new MemoryStream(); webPImage.Save(webPMemoryStream, ImageFormat.Png); stream = webPMemoryStream; } stream.Seek(0, SeekOrigin.Begin); try { var bitmap = new BitmapImage(); bitmap.BeginInit(); bitmap.StreamSource = stream; bitmap.EndInit(); bitmap.Freeze(); context.UIContext.Send(state => { context.AttachSource(bitmap); }, null); tcs.SetResult(bitmap); } catch (Exception ex) { tcs.SetException(ex); } }, cancellationToken); context.Current = await tcs.Task; } await next(context, cancellationToken); }
private async void ConvertWebPToImage(string path, ISupportedImageFormat format, string formatString) { byte[] b_image; try { b_image = File.ReadAllBytes(path); } catch (Exception e) { await this.ShowMessageAsync("错误", "读入图片时发生错误: " + e.Message, MessageDialogStyle.Affirmative, generalDialogSetting); return; } using (MemoryStream inStream = new MemoryStream(b_image)) { using (ImageFactory imageFactory = new ImageFactory(preserveExifData: true)) { string savePath = Path.GetDirectoryName(path) + @"/" + Path.GetFileNameWithoutExtension(path) + formatString; if (File.Exists(savePath)) { var existDialogRes = await this.ShowMessageAsync("文件已存在", "将要保存的文件: " + savePath + " 已存在,是否覆盖?", MessageDialogStyle.AffirmativeAndNegative, new MetroDialogSettings() { AffirmativeButtonText = "覆盖", NegativeButtonText = "取消", DialogTitleFontSize = 16 }); if (!(existDialogRes == MessageDialogResult.Affirmative)) { return; } } try { WebPFormat webpFormat = new WebPFormat(); imageFactory.Load(webpFormat.Load(inStream)).Format(format).Save(savePath); } catch (Exception e) { await this.ShowMessageAsync("错误", "转换时发生错误: " + e.Message, MessageDialogStyle.Affirmative, generalDialogSetting); return; } await this.ShowMessageAsync("转换完成", "图片已转换完成。", MessageDialogStyle.Affirmative, generalDialogSetting); } } }
public static Bitmap loadBitmap(string path, string extension, int sizeX = -1, int sizeY = -1) { Bitmap bitmap; if (extension == ".webp") { WebPFormat webpDecoder = new WebPFormat(); bitmap = (Bitmap)webpDecoder.Load(File.Open(path, FileMode.Open)); } else { if (sizeX == -1 || sizeY == -1) { bitmap = new Bitmap(Image.FromFile(path), new Size(Settings.Accuracy, Settings.Accuracy)); } else { bitmap = new Bitmap(Image.FromFile(path), new Size(sizeX, sizeY)); } } return(bitmap); }