/// <summary> /// 生成缩略图到MemoryStream /// </summary> /// <param name="SourceFile"></param> /// <param name="intThumbWidth"></param> /// <param name="intThumbHeight"></param> /// <param name="gap">是否补足空白</param> public static MemoryStream ThumbImageToStream(string SourceFile, int intThumbWidth, int intThumbHeight, bool gap = true) { MemoryStream ms = new MemoryStream(); //原图加载 using (System.DrawingCore.Image sourceImage = System.DrawingCore.Image.FromFile(SourceFile)) { //是否显示原图 if (intThumbWidth == 0 && intThumbHeight == 0) { sourceImage.Save(ms, System.DrawingCore.Imaging.ImageFormat.Png); return(ms); } //原图宽度和高度 int width = sourceImage.Width; int height = sourceImage.Height; int smallWidth; int smallHeight; //如果原图长宽均比缩略图的要小则返回原stream if (width <= intThumbWidth && height <= intThumbHeight) { sourceImage.Save(ms, System.DrawingCore.Imaging.ImageFormat.Png); return(ms); } //获取第一张绘制图的大小,(比较 原图的宽/缩略图的宽 和 原图的高/缩略图的高) if (((decimal)width) / height <= ((decimal)intThumbWidth) / intThumbHeight) { smallWidth = intThumbHeight * width / height; smallHeight = intThumbHeight; } else { smallWidth = intThumbWidth; smallHeight = intThumbWidth * height / width; } Image newimg = sourceImage.GetThumbnailImage(smallWidth, smallHeight, null, IntPtr.Zero); //使用原宽高比输出,不补足空白 if (!gap) { newimg.Save(ms, System.DrawingCore.Imaging.ImageFormat.Png); return(ms); } //新建一个图板,以最小等比例压缩大小绘制原图 using (System.DrawingCore.Image bitmap = new System.DrawingCore.Bitmap(smallWidth, smallHeight)) { //绘制中间图 using (System.DrawingCore.Graphics g = System.DrawingCore.Graphics.FromImage(bitmap)) { //高清,平滑 g.InterpolationMode = System.DrawingCore.Drawing2D.InterpolationMode.High; g.SmoothingMode = System.DrawingCore.Drawing2D.SmoothingMode.HighQuality; g.Clear(Color.Transparent); g.DrawImage( sourceImage, new System.DrawingCore.Rectangle(0, 0, smallWidth, smallHeight), new System.DrawingCore.Rectangle(0, 0, width, height), System.DrawingCore.GraphicsUnit.Pixel ); } //新建一个图板,以缩略图大小绘制中间图 using (System.DrawingCore.Image bitmap1 = new System.DrawingCore.Bitmap(intThumbWidth, intThumbHeight)) { //绘制缩略图 using (System.DrawingCore.Graphics g = System.DrawingCore.Graphics.FromImage(bitmap1)) { //高清,平滑 g.InterpolationMode = System.DrawingCore.Drawing2D.InterpolationMode.High; g.SmoothingMode = System.DrawingCore.Drawing2D.SmoothingMode.HighQuality; g.Clear(Color.Transparent); int lwidth = (smallWidth - intThumbWidth) / 2; int bheight = (smallHeight - intThumbHeight) / 2; g.DrawImage(bitmap, new Rectangle(0, 0, intThumbWidth, intThumbHeight), lwidth, bheight, intThumbWidth, intThumbHeight, GraphicsUnit.Pixel); g.Dispose(); bitmap1.Save(ms, System.DrawingCore.Imaging.ImageFormat.Png); return(ms); } } } } }
override internal void RunPage(Pages pgs, Row row) { Report r = pgs.Report; bool bHidden = IsHidden(r, row); WorkClass wc = GetWC(r); string mtype = null; Stream strm = null; System.DrawingCore.Image im = null; SetPagePositionBegin(pgs); if (bHidden) { PageImage pi = new PageImage(ImageFormat.Jpeg, null, 0, 0); this.SetPagePositionAndStyle(r, pi, row); SetPagePositionEnd(pgs, pi.Y + pi.H); return; } if (wc.PgImage != null) { // have we already generated this one // reuse most of the work; only position will likely change PageImage pi = new PageImage(wc.PgImage.ImgFormat, wc.PgImage.ImageData, wc.PgImage.SamplesW, wc.PgImage.SamplesH); pi.Name = wc.PgImage.Name; // this is name it will be shared under pi.Sizing = this._Sizing; this.SetPagePositionAndStyle(r, pi, row); pgs.CurrentPage.AddObject(pi); SetPagePositionEnd(pgs, pi.Y + pi.H); return; } try { strm = GetImageStream(r, row, out mtype); if (strm == null) { r.rl.LogError(4, string.Format("Unable to load image {0}.", this.Name.Nm)); return; } im = System.DrawingCore.Image.FromStream(strm); int height = im.Height; int width = im.Width; MemoryStream ostrm = new MemoryStream(); strm.Position = 0; ImageFormat imf; switch (mtype.ToLower()) { case "image/jpeg": imf = ImageFormat.Jpeg; CopyStream(strm, ostrm); break; case "image/png": imf = ImageFormat.Png; CopyStream(strm, ostrm); break; default: // from old code where all images convert to jpeg, i don't know why. May be need delete it and add all support formats. imf = ImageFormat.Jpeg; System.DrawingCore.Imaging.ImageCodecInfo[] info; info = ImageCodecInfo.GetImageEncoders(); EncoderParameters encoderParameters; encoderParameters = new EncoderParameters(1); encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, ImageQualityManager.EmbeddedImageQuality); System.DrawingCore.Imaging.ImageCodecInfo codec = null; for (int i = 0; i < info.Length; i++) { if (info[i].FormatDescription == "JPEG") { codec = info[i]; break; } } im.Save(ostrm, codec, encoderParameters); break; } byte[] ba = ostrm.ToArray(); ostrm.Close(); PageImage pi = new PageImage(imf, ba, width, height); pi.Sizing = this._Sizing; this.SetPagePositionAndStyle(r, pi, row); pgs.CurrentPage.AddObject(pi); if (_ConstantImage) { wc.PgImage = pi; // create unique name; PDF generation uses this to optimize the saving of the image only once pi.Name = "pi" + Interlocked.Increment(ref Parser.Counter).ToString(); // create unique name } SetPagePositionEnd(pgs, pi.Y + pi.H); } catch (Exception e) { // image failed to load, continue processing r.rl.LogError(4, "Image load failed. " + e.Message); } finally { if (strm != null) { strm.Close(); } if (im != null) { im.Dispose(); } } return; }
/// <summary> /// 增加图片文字水印 /// </summary> /// <param name="filename">文件名</param> /// <param name="watermarkText">水印文字</param> /// <param name="watermarkStatus">图片水印位置</param> public static void AddImageSignText(System.DrawingCore.Image img, string filename, string watermarkText, int watermarkStatus, int quality, string fontname, int fontsize) { Graphics g = Graphics.FromImage(img); Font drawFont = new Font(fontname, fontsize, FontStyle.Regular, GraphicsUnit.Pixel); SizeF crSize; crSize = g.MeasureString(watermarkText, drawFont); float xpos = 0; float ypos = 0; switch (watermarkStatus) { case 1: xpos = (float)img.Width * (float).01; ypos = (float)img.Height * (float).01; break; case 2: xpos = ((float)img.Width * (float).50) - (crSize.Width / 2); ypos = (float)img.Height * (float).01; break; case 3: xpos = ((float)img.Width * (float).99) - crSize.Width; ypos = (float)img.Height * (float).01; break; case 4: xpos = (float)img.Width * (float).01; ypos = ((float)img.Height * (float).50) - (crSize.Height / 2); break; case 5: xpos = ((float)img.Width * (float).50) - (crSize.Width / 2); ypos = ((float)img.Height * (float).50) - (crSize.Height / 2); break; case 6: xpos = ((float)img.Width * (float).99) - crSize.Width; ypos = ((float)img.Height * (float).50) - (crSize.Height / 2); break; case 7: xpos = (float)img.Width * (float).01; ypos = ((float)img.Height * (float).99) - crSize.Height; break; case 8: xpos = ((float)img.Width * (float).50) - (crSize.Width / 2); ypos = ((float)img.Height * (float).99) - crSize.Height; break; case 9: xpos = ((float)img.Width * (float).99) - crSize.Width; ypos = ((float)img.Height * (float).99) - crSize.Height; break; } g.DrawString(watermarkText, drawFont, new SolidBrush(Color.White), xpos + 1, ypos + 1); g.DrawString(watermarkText, drawFont, new SolidBrush(Color.Black), xpos, ypos); ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo ici = null; foreach (ImageCodecInfo codec in codecs) { if (codec.MimeType.IndexOf("jpeg") > -1) { ici = codec; } } EncoderParameters encoderParams = new EncoderParameters(); long[] qualityParam = new long[1]; if (quality < 0 || quality > 100) { quality = 80; } qualityParam[0] = quality; EncoderParameter encoderParam = new EncoderParameter(System.DrawingCore.Imaging.Encoder.Quality, qualityParam); encoderParams.Param[0] = encoderParam; if (ici != null) { img.Save(filename, ici, encoderParams); } else { img.Save(filename); } g.Dispose(); img.Dispose(); }