/// <summary> /// 生成页面预览 /// </summary> /// <param name="pObj"></param> /// <param name="strSavePath"></param> /// <returns></returns> public static bool CreatePageViewImage(PageDataObj pObj, string strSavePath, int orgWidth, int orgHeight, int outWidth, int outHeight, ref bool istxtOverflow) { Bitmap bitmapOutput = new Bitmap(orgWidth, orgHeight); Graphics g = Graphics.FromImage(bitmapOutput); Color c = ColorTranslator.FromHtml(pObj.bgcolor); g.Clear(c); EncoderParameter p = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L); EncoderParameters ps = new EncoderParameters(1); ps.Param[0] = p; if (pObj.image != null) { foreach (PageImage item in pObj.image) { float wScale = item.orgwidth / (float)item.conwidth; float hScale = item.orgheight / (float)item.conheight; float floZoom = wScale > hScale ? hScale : wScale; int intZoomWidth = Convert.ToInt32(item.conwidth * floZoom); int intZoomHeight = Convert.ToInt32(item.conheight * floZoom); int intOutputWidth = Convert.ToInt32(item.conwidth); int intOutputHeight = Convert.ToInt32(item.conheight); int startX = Convert.ToInt32(item.x * floZoom) * -1; int startY = Convert.ToInt32(item.y * floZoom) * -1; string strImageUrl = GetEditLocationUrl(item.src); Bitmap bitmapSource = new Bitmap(strImageUrl); Bitmap bitmapZoom = new Bitmap(intZoomWidth, intZoomHeight); Bitmap bitmapCut = new Bitmap(intOutputWidth, intOutputHeight); CutPhoto(ref bitmapZoom, bitmapSource, new System.Drawing.Rectangle(0, 0, intZoomWidth, intZoomHeight), startX, startY, intZoomWidth, intZoomHeight); CutPhoto(ref bitmapCut, bitmapZoom, new System.Drawing.Rectangle(0, 0, intOutputWidth, intOutputHeight), 0, 0, intZoomWidth, intZoomHeight); g.DrawImage(bitmapCut, item.conx, item.cony, intOutputWidth, intOutputHeight); g.Dispose(); bitmapSource.Dispose(); bitmapCut.Dispose(); } } AddText2PageView(bitmapOutput, pObj, ref istxtOverflow); Bitmap outImage = new Bitmap(outWidth, outHeight); CutPhoto(ref outImage, bitmapOutput, new System.Drawing.Rectangle(0, 0, outWidth, outHeight), 0, 0, orgWidth, orgHeight); outImage.Save(strSavePath, GetCodecInfo("image/jpeg"), ps); outImage.Dispose(); return(true); }
/// <summary> /// 修改书本图片使用次数 /// </summary> /// <param name="pdata"></param> /// <param name="bookid"></param> /// <param name="isReduce"></param> /// <returns></returns> public static BaseResponse ChangeImageUsedNum(XElement pdata, int bookid, bool isReduce, ref List <int> imageids) { BaseResponse br = new BaseResponse(); br.IsSuccess = false; try { int baseNum = isReduce ? -1 : 1; PageDataObj pageObj = (PageDataObj)SerializeXmlHelper.DeserializeFromXml(pdata, typeof(PageDataObj)); if (pageObj.image != null) { foreach (PageImage img in pageObj.image) { Inpinke_Book_Image model = InpinkeDataContext.Instance.Inpinke_Book_Images.Get(e => e.BookID == bookid && e.ImageID == img.imageid); if (model != null) { model.UsedNum += baseNum; if (model.UsedNum < 0) { model.UsedNum = 0; } if (isReduce && model.UsedNum == 0) { imageids.Add(model.ImageID); } else if (!isReduce && model.UsedNum == 1) { imageids.Add(model.ImageID); } model.SaveWhenSubmit(InpinkeDataContext.Instance); } } InpinkeDataContext.Instance.Submit(); } br.IsSuccess = true; return(br); } catch (Exception ex) { br.IsSuccess = false; br.Message = "修改书本图片使用次数失败,请稍后再试"; Logger.Error(string.Format("ChangeImageUsedNum BookID:{0},Error:{1}", bookid, ex.ToString())); return(br); } }
/// <summary> /// 保存页面缩略图 /// </summary> /// <param name="path"></param> /// <param name="pobj"></param> public string SavePageView(PageDataObj pobj, int userid, Inpinke_Book book, ref bool istxtO, int pageid) { int bookid = book.ID; string prodName = book.Inpinke_Product.ShortName.ToString().ToLower(); string dName = "/UserFile"; string path = Server.MapPath(dName); string filePath = path + "/" + userid + "/" + bookid; if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } string fileName = "pview_" + pageid + ".jpg"; int orgWidth = ConfigMap.GetEditorAttr(prodName, "EditorWidth"); orgWidth = pobj.isskip == "true" ? orgWidth * 2 : orgWidth; int orgHeight = ConfigMap.GetEditorAttr(prodName, "EditorHeight"); int outWidth = ConfigMap.GetEditorAttr(prodName, "MiniViewWidth"); outWidth = pobj.isskip == "true" ? outWidth * 2 : outWidth; int outHeight = ConfigMap.GetEditorAttr(prodName, "MiniViewHeight"); ImageProcessBLL.CreatePageViewImage(pobj, filePath + "/" + fileName, orgWidth, orgHeight, outWidth, outHeight, ref istxtO); ImageProcessBLL.CreatePageViewImage(pobj, filePath + "/pthumb_" + pageid + ".jpg", orgWidth, orgHeight, orgWidth, orgHeight, ref istxtO); string returnName = dName + "/" + userid + "/" + bookid + "/" + fileName; if (pobj.pagenum == 0) { Image originalImage = Image.FromFile(filePath + "/pthumb_" + pageid + ".jpg"); Bitmap bitmap = new Bitmap(originalImage); ImageProcessBLL.CreateScaleImage(bitmap, 200, 200, filePath + "/cover200.jpg", false); originalImage.Dispose(); } return(returnName); }
/// <summary> /// 添加文字到预览图片 /// </summary> /// <param name="bitmap"></param> /// <param name="pdata"></param> /// <param name="isTxtOverflow">文字是否溢出</param> public static void AddText2PageView(Bitmap bitmap, PageDataObj pdata, ref bool isTxtOverflow) { Graphics g = Graphics.FromImage(bitmap); g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; foreach (PageText t in pdata.text) { System.Drawing.Font font = new System.Drawing.Font("方正兰亭黑_GBK", t.fontsize, GraphicsUnit.Pixel); float x = t.conx; float y = t.cony; if (!string.IsNullOrEmpty(t.content)) { StringFormat sf = new StringFormat(); sf.Alignment = t.textalign == "left" ? StringAlignment.Near : t.textalign == "center" ? StringAlignment.Center : StringAlignment.Far; string printT = ""; int len = t.content.Length; Brush b = t.color.ToLower() == "#ffffff" ? Brushes.White : Brushes.Black; int line = 0; if (t.issingle == "true") { SizeF fsize2 = g.MeasureString(t.content, font); isTxtOverflow = fsize2.Width > t.conwidth; g.DrawString(t.content, font, b, new RectangleF(x, y, t.conwidth, t.conheight), sf); continue; } string p = @"[].,,。;;》>、!!??】}}]"; for (int i = 0; i < len; i++) { printT += t.content[i]; SizeF fsize = g.MeasureString(printT, font); if (i + 1 < len) { fsize = g.MeasureString(printT + t.content[i + 1], font); if (Regex.IsMatch(t.content[i + 1].ToString(), p)) { printT += t.content[i + 1]; i++; } } if (fsize.Width > t.conwidth || printT.Contains("\n")) { if (printT.Contains("\n")) { printT = printT.Replace("\n", ""); fsize.Height = fsize.Height / 2; } g.DrawString(printT, font, b, new RectangleF(x, y, t.conwidth, fsize.Height), sf); y += fsize.Height + 5; printT = ""; line++; } if (y + fsize.Height > t.cony + t.conheight) { isTxtOverflow = true; continue; } } if (!string.IsNullOrEmpty(printT)) { SizeF fsize1 = g.MeasureString(printT, font); g.DrawString(printT, font, b, new RectangleF(x, y, t.conwidth, fsize1.Height), sf); } isTxtOverflow = false; //SizeF fsize = g.MeasureString(t.content, font,new SizeF(t.conwidth,t.conheight),sf,); //x = t.textalign == "left" ? x : t.textalign == "center" ? x + (t.conwidth - fsize.Width) / 2 : x + (t.conwidth - fsize.Width); //g.DrawString(t.content, font, t.color.ToLower() == "#ffffff" ? Brushes.White : Brushes.Black, new RectangleF(x, y, t.conwidth, t.conheight), sf); } } g.Dispose(); }
/// <summary> /// 生成书本pdf /// </summary> /// <param name="bookid"></param> /// <returns></returns> public static bool CreateBookPDF(int bookid) { Inpinke_Book model = DBBookBLL.GetBookByID(bookid); string pdfname = FilterSpecial(model.BookName); pdfname = OutPath + pdfname + "-" + model.ID + "-intime.pdf"; PDFProcessBLL pdfProcess = new PDFProcessBLL(PageWidth + 2 * TrimLineLength, PageHeight + 2 * TrimLineLength, pdfname); try { IList <Inpinke_Book_Page> pages = DBBookBLL.GetBookPage(bookid); pdfProcess.PaintScale = PaintScale; if (pages != null) { pages = pages.OrderBy(e => e.PageNum).ToList(); pdfProcess.doc.Open(); float boneWidth = backboneWidth * model.PageCount; pdfProcess.FlodPageWidth = flodPageWidth; pdfProcess.BackBoneWidth = boneWidth; pdfProcess.SinglePageWidth = PageWidth; pdfProcess.SinglePageHeight = PageHeight; for (int i = 0; i <= model.PageCount; i++) { Inpinke_Book_Page p = pages.Where(e => e.PageNum == i).SingleOrDefault(); if (p == null) { pdfProcess.PageWidth = PageWidth + 2 * TrimLineLength; pdfProcess.PageHeight = PageHeight + 2 * TrimLineLength; pdfProcess.doc.SetPageSize(new iTextSharp.text.Rectangle(pdfProcess.PageWidth, pdfProcess.PageHeight)); pdfProcess.doc.NewPage(); pdfProcess.PaintTirmLine(); } else { float pWidth = PageWidth; if (p.IsSkip) { pWidth = PageWidth * 2; if (i != 0) { i++; } } else if (i != 1 && i != model.PageCount) { pWidth = PageWidth * 2; } if (i == 0) { //封面和封底和折页书脊总宽度 pWidth = PageWidth * 2 + flodPageWidth * 2 + boneWidth; } pdfProcess.PageWidth = pWidth + 2 * TrimLineLength; pdfProcess.PageHeight = PageHeight + 2 * TrimLineLength; pdfProcess.doc.SetPageSize(new iTextSharp.text.Rectangle(pdfProcess.PageWidth, pdfProcess.PageHeight)); pdfProcess.doc.NewPage(); pdfProcess.PaintTirmLine(); PageDataObj pageObj = (PageDataObj)SerializeXmlHelper.DeserializeFromXml(p.PageData, typeof(PageDataObj)); pdfProcess.PaintPage(pageObj, 0); if (!p.IsSkip && i != 1 && i != model.PageCount) { p = pages.Where(e => e.PageNum == i + 1).SingleOrDefault(); if (p != null) { pageObj = (PageDataObj)SerializeXmlHelper.DeserializeFromXml(p.PageData, typeof(PageDataObj)); pdfProcess.PaintPage(pageObj, PageWidth); i++; } } } } pdfProcess.doc.Close(); } return(true); } catch (Exception ex) { pdfProcess.doc.Close(); Logger.Error(string.Format("CreateBookPDF BookID:{0},Error:{1}", bookid, ex.ToString())); return(false); } }
/// <summary> /// 输出页面 /// </summary> public void PaintPage(PageDataObj pObj, float pStartX) { if (pObj.pnum == this.CoverPNum) { PaintCoverPage(pObj); return; } if (!string.IsNullOrEmpty(pObj.bgcolor) && pObj.bgcolor.ToUpper() != "#FFFFFF") { PaintBGColor(pObj.bgcolor, TrimLineLength, TrimLineLength, PageWidth - 2 * TrimLineLength, PageHeight - 2 * TrimLineLength); } if (pObj.text != null) { foreach (PageText ptxt in pObj.text) { float textX = ptxt.conx / PaintScale + TrimLineLength + pStartX; float textWidth = ptxt.conwidth / PaintScale; float textHeight = ptxt.conheight / PaintScale; float textY = ptxt.cony / PaintScale + TrimLineLength; float textTop = ptxt.cony / PaintScale + TrimLineLength; FontFactory.Register(fontPath[0]); BaseFont bfChinese = BaseFont.CreateFont(fontPath[0], BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); float fontsize = ptxt.fontsize; int align = ptxt.textalign == "left" ? Element.ALIGN_LEFT : ptxt.textalign == "right" ? Element.ALIGN_RIGHT : ptxt.textalign == "center" ? Element.ALIGN_CENTER : Element.ALIGN_LEFT; if (ptxt.issingle == "true") { textY = PageHeight - ptxt.cony / PaintScale - TrimLineLength - textHeight; fontsize = 8f; if (pObj.pnum == 0) { fontsize = ptxt.conid == "txt_1" ? 27f : 15f; } PaintContent(ptxt.color, ptxt.content, fontPath[0], fontsize, textHeight / 1.5f, 0, textX, textY, textHeight, textWidth, align); } else { PaintContent(ptxt.color, ptxt.content, fontPath[0], fontsize, 17f, 0, textX, textY, textHeight, textWidth, align); } } } if (pObj.image != null) { foreach (PageImage pimg in pObj.image) { float imageX = pimg.conx / PaintScale + TrimLineLength + pStartX; float imageWidth = pimg.conwidth / PaintScale; float imageHeight = pimg.conheight / PaintScale; float imageY = PageHeight - pimg.cony / PaintScale - TrimLineLength - imageHeight; float floZoom = pimg.orgheight / pimg.height; int intZoomWidth = Convert.ToInt32(pimg.conwidth * floZoom * 4); int intZoomHeight = Convert.ToInt32(pimg.conheight * floZoom * 4); int intOutputWidth = (int)((imageWidth) / DpiScale); int intOutputHeight = (int)((imageHeight) / DpiScale); int startX = Convert.ToInt32(pimg.x * floZoom * 4) * -1; int startY = Convert.ToInt32(pimg.y * floZoom * 4) * -1; string strImageUrl = GetLocationUrl(pimg.src); Bitmap bitmapSource = new Bitmap(strImageUrl); Bitmap bitmapCut = new Bitmap(intZoomWidth, intZoomHeight); Bitmap bitmapZoom = new Bitmap(intOutputWidth, intOutputHeight); CutPhoto(ref bitmapCut, bitmapSource, new System.Drawing.Rectangle(0, 0, intZoomWidth, intZoomHeight), startX, startY, intZoomWidth, intZoomHeight); CutPhoto(ref bitmapZoom, bitmapCut, new System.Drawing.Rectangle(0, 0, intOutputWidth, intOutputHeight), 0, 0, intZoomWidth, intZoomHeight); PaintImage(bitmapZoom, imageX, imageY); bitmapSource.Dispose(); bitmapCut.Dispose(); } } }