public static Stream GenerateFilePreview(int fileHW, Stream stream, ImageModelEnum model, ref int width, ref int height) { string type = GetImageType2(stream); bool isGif = type == "GIF"; if (type == "XML") { width = fileHW; height = fileHW; return(GenerateSvg(stream, model, ref width, ref height)); } else { using (Image image = Image.FromStream(stream)) { //原图比较宽 if (image.Width >= image.Height) { width = image.Width > fileHW ? fileHW : image.Width; //原图比指定的宽度要宽,就是用指定的宽度,否则使用原图宽 height = image.Height * width / image.Width; } //原图比较高 if (image.Width < image.Height) { height = image.Height > fileHW ? fileHW : image.Height; //原图比指定的高度要高,就是用指定的高度,否则使用原图高 width = image.Width * height / image.Height; } return(ConvertImage(image, image.RawFormat, 0, 0, width, height, false)); } } }
public static Stream GenerateThumbnail(Stream stream, ImageModelEnum model, int x, int y, ref int width, ref int height) { string type = GetImageType2(stream); bool cut = false; if (type == "XML") { return(GenerateSvg(stream, model, ref width, ref height)); } else { using (Image image = Image.FromStream(stream)) { switch (model) { case ImageModelEnum.scale: if (width == 0 && height == 0) { width = image.Width; height = image.Height; } else if (width == 0 && height > 0) { width = image.Width * height / image.Height; } else if (width > 0 && height == 0) { height = image.Height * width / image.Width; } break; case ImageModelEnum.height: width = image.Width * height / image.Height; break; case ImageModelEnum.width: height = image.Height * width / image.Width; break; case ImageModelEnum.cut: cut = true; break; } if (width > image.Width) { width = image.Width; } if (height > image.Height) { height = image.Height; } return(ConvertImage(image, image.RawFormat, x, y, width, height, cut)); } } }
private static Stream GenerateSvg(Stream stream, ImageModelEnum model, ref int width, ref int height) { string text = stream.ToStr(); string pattern = @"<svg\s*(.|\n)+?>"; string widthPattern = @"width=""(\d+)(px)?"""; string heightPattern = @"height=""(\d+)(px)?"""; string svgTag = Regex.Match(text, pattern).Groups[0].Value; string newSvgTag = svgTag; var widthMath = Regex.Match(svgTag, widthPattern); int swidth = widthMath.Success ? int.Parse(widthMath.Groups[1].Value) : 1024; var heightMath = Regex.Match(svgTag, heightPattern); int sheight = heightMath.Success ? int.Parse(widthMath.Groups[1].Value) : 1024; switch (model) { case ImageModelEnum.scale: if (width == 0 && height == 0) { width = swidth; height = sheight; } else if (width == 0 && height > 0) { width = swidth * height / sheight; } else if (width > 0 && height == 0) { height = sheight * width / swidth; } break; case ImageModelEnum.height: if (sheight != 0 && swidth != 0) { width = swidth * height / sheight; } else { width = height; } break; case ImageModelEnum.width: if (sheight != 0 && swidth != 0) { height = sheight * width / swidth; } else { height = width; } break; } if (newSvgTag.Contains("width")) { newSvgTag = Regex.Replace(newSvgTag, widthPattern, "width=\"" + width + "\""); } else { newSvgTag = newSvgTag.TrimEnd('>') + " width=\"" + width + "\"" + ">"; } if (newSvgTag.Contains("height")) { newSvgTag = Regex.Replace(newSvgTag, heightPattern, "height=\"" + height + "\""); } else { newSvgTag = newSvgTag.TrimEnd('>') + " height=\"" + height + "\"" + ">"; } text = text.Replace(svgTag, newSvgTag); return(text.ToStream()); }