public void StartGenerate(RssInfo rssInfo, RssMedia rssMedia, out List <System.Drawing.Image> imgList) { imgList = new List <System.Drawing.Image>(); ImageObj obj = new ImageObj(rssInfo, rssMedia); Thread thread = new Thread(GetImageFromRss); try { threadDic.Add(rssMedia.CachePath, thread); thread.Start(obj); } catch (ArgumentException aex) { Trace.WriteLine("rss生成图片线程遇到bug:" + aex.Message); } }
/// <summary> /// 从rss解析到每一页中每一行的数据 /// </summary> /// <param name="channel"></param> /// <returns></returns> private List <Page> GetPageListFromRss(RssInfo rssInfo, RssMedia rssMedia) { List <Paragraph> paragraphs = new List <Paragraph>(); foreach (RssItemInfo item in rssInfo.RssItem) { if (item != null) { if (rssMedia.IsShowRssTitle) { paragraphs.AddRange(GetCharacter(GetCleanString(item.Title), rssMedia.RssTitleProp.TextFont, rssMedia.RssTitleProp.TextColor, RssBodyType.Title)); } if (rssMedia.IsShowRssPublishTime) { paragraphs.AddRange(GetCharacter(GetCleanString(item.PubDate), rssMedia.RssPublishTimeProp.TextFont, rssMedia.RssPublishTimeProp.TextColor, RssBodyType.Time)); } if (rssMedia.IsShowRssBody) { if (item.Description != null) { foreach (var v in item.Description) { paragraphs.AddRange(GetCharacter(GetCleanString(v.content), rssMedia.RssBodyProp.TextFont, rssMedia.RssPublishTimeProp.TextColor, RssBodyType.Body)); } } } } } List <Page> pageList = GetPageList(paragraphs, pageWidth, pageHeight, rssMedia); paragraphs.Clear(); paragraphs = null; return(pageList); }
public ImageObj(RssInfo rssInfo, RssMedia rssMedia) { this.rssInfo = rssInfo; this.rssMedia = rssMedia; }
/// <summary> /// 从rss生成图像 /// </summary> /// <param name="channel">从rss源获得的数据</param> /// <param name="path">保存图像的路径</param> private void GetImageFromRss(object obj) { ImageObj image = (ImageObj)obj; RssInfo rssInfo = image.rssInfo; RssMedia rssMedia = image.rssMedia; string fileName = ""; if (rssInfo == null || rssMedia == null) { return; } List <Page> pages = GetPageListFromRss(rssInfo, rssMedia); var wicFactory = new ImagingFactory(); var d2dFactory = new SharpDX.Direct2D1.Factory(); var dwFactory = new SharpDX.DirectWrite.Factory(); var renderTargetProperties = new RenderTargetProperties(RenderTargetType.Default, new SharpDX.Direct2D1.PixelFormat(Format.Unknown, SharpDX.Direct2D1.AlphaMode.Unknown), 0, 0, RenderTargetUsage.None, FeatureLevel.Level_DEFAULT); var wicBitmap = new SharpDX.WIC.Bitmap(wicFactory, pageWidth, pageHeight, SharpDX.WIC.PixelFormat.Format32bppBGR, BitmapCreateCacheOption.CacheOnLoad); var d2dRenderTarget = new WicRenderTarget(d2dFactory, wicBitmap, renderTargetProperties); d2dRenderTarget.AntialiasMode = AntialiasMode.PerPrimitive; var solidColorBrush = new SolidColorBrush(d2dRenderTarget, new SharpDX.Color(this.backgroundColor.R, this.backgroundColor.G, this.backgroundColor.B, this.backgroundColor.A)); var textBodyBrush = new SolidColorBrush(d2dRenderTarget, new SharpDX.Color(rssMedia.RssBodyProp. TextColor.R, rssMedia.RssBodyProp.TextColor.G, rssMedia.RssBodyProp.TextColor.B, rssMedia.RssBodyProp.TextColor.A)); var titleColorBrush = new SolidColorBrush(d2dRenderTarget, new SharpDX.Color(rssMedia.RssTitleProp.TextColor.R, rssMedia.RssTitleProp.TextColor.G, rssMedia.RssTitleProp.TextColor.B, rssMedia.RssTitleProp.TextColor.A)); var publishDateColorBrush = new SolidColorBrush(d2dRenderTarget, new SharpDX.Color(rssMedia.RssPublishTimeProp.TextColor.R, rssMedia.RssPublishTimeProp.TextColor.G, rssMedia.RssPublishTimeProp.TextColor.B, rssMedia.RssPublishTimeProp.TextColor.A)); TextLayout textLayout; try { int count = 0; foreach (Page page in pages) { d2dRenderTarget.BeginDraw(); d2dRenderTarget.Clear(new SharpDX.Color(this.backgroundColor.R, this.backgroundColor.G, this.backgroundColor.B, this.backgroundColor.A)); foreach (Line line in page.lines) { foreach (Block block in line.content) { TextFormat textFormat = new TextFormat(dwFactory, block.font.FontFamily.Name, block.font.Size); textLayout = new TextLayout(dwFactory, block.content, textFormat, block.width, block.height); switch (block.type) { case RssBodyType.Title: textLayout.SetUnderline(rssMedia.RssTitleProp.TextFont.Underline, new TextRange(0, block.content.Length)); textLayout.SetFontStyle(rssMedia.RssTitleProp.TextFont.Italic ? FontStyle.Italic : FontStyle.Normal, new TextRange(0, block.content.Length)); textLayout.SetFontWeight(rssMedia.RssTitleProp.TextFont.Bold ? FontWeight.Bold : FontWeight.Normal, new TextRange(0, block.content.Length)); d2dRenderTarget.DrawTextLayout(new Vector2(block.left, block.top), textLayout, titleColorBrush); break; case RssBodyType.Time: textLayout.SetUnderline(rssMedia.RssPublishTimeProp.TextFont.Underline, new TextRange(0, block.content.Length)); textLayout.SetFontStyle(rssMedia.RssPublishTimeProp.TextFont.Italic ? FontStyle.Italic : FontStyle.Normal, new TextRange(0, block.content.Length)); textLayout.SetFontWeight(rssMedia.RssPublishTimeProp.TextFont.Bold ? FontWeight.Bold : FontWeight.Normal, new TextRange(0, block.content.Length)); d2dRenderTarget.DrawTextLayout(new Vector2(block.left, block.top), textLayout, publishDateColorBrush); break; case RssBodyType.Body: textLayout.SetUnderline(rssMedia.RssBodyProp.TextFont.Underline, new TextRange(0, block.content.Length)); textLayout.SetFontStyle(rssMedia.RssBodyProp.TextFont.Italic ? FontStyle.Italic : FontStyle.Normal, new TextRange(0, block.content.Length)); textLayout.SetFontWeight(rssMedia.RssBodyProp.TextFont.Bold ? FontWeight.Bold : FontWeight.Normal, new TextRange(0, block.content.Length)); d2dRenderTarget.DrawTextLayout(new Vector2(block.left, block.top), textLayout, textBodyBrush); break; default: break; } textFormat.Dispose(); textFormat = null; } } d2dRenderTarget.EndDraw(); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } fileName = string.Format("{0}{1}.jpg", path, DateTime.Now.Ticks.ToString()); var stream = new WICStream(wicFactory, fileName, NativeFileAccess.Write); var encoder = new PngBitmapEncoder(wicFactory); encoder.Initialize(stream); var bitmapFrameEncode = new BitmapFrameEncode(encoder); bitmapFrameEncode.Initialize(); bitmapFrameEncode.SetSize(pageWidth, pageHeight); var pixelFormatGuid = SharpDX.WIC.PixelFormat.FormatDontCare; bitmapFrameEncode.SetPixelFormat(ref pixelFormatGuid); bitmapFrameEncode.WriteSource(wicBitmap); bitmapFrameEncode.Commit(); encoder.Commit(); bitmapFrameEncode.Dispose(); encoder.Dispose(); stream.Dispose(); Console.WriteLine("*********image count is : " + count++); //发送单个图片生成事件 if (SingleGenerateCompleteEvent != null) { SingleGenerateCompleteEvent(fileName); } } //发送生成完成事件 if (GenerateCompleteEvent != null) { GenerateCompleteEvent(path); //停止线程,从字典删除 StopGenerate(rssMedia.CachePath); } } catch (ThreadAbortException aborted) { Trace.WriteLine("rss 图片生成线程终止 : " + aborted.Message); } catch (Exception ex) { Trace.WriteLine("rss 图片生成遇到bug : " + ex.Message); } finally { wicFactory.Dispose(); d2dFactory.Dispose(); dwFactory.Dispose(); wicBitmap.Dispose(); d2dRenderTarget.Dispose(); solidColorBrush.Dispose(); textBodyBrush.Dispose(); titleColorBrush.Dispose(); publishDateColorBrush.Dispose(); rssInfo.Dispose(); rssMedia.Dispose(); wicFactory = null; d2dFactory = null; dwFactory = null; wicBitmap = null; d2dRenderTarget = null; solidColorBrush = null; textBodyBrush = null; titleColorBrush = null; publishDateColorBrush = null; rssInfo = null; rssMedia = null; pages.Clear(); pages = null; } }
/// <summary> /// 由字符段落获得图像页面 /// </summary> /// <param name="paragraphList"></param> /// <param name="pageWidth"></param> /// <param name="pageHeight"></param> /// <returns></returns> private List <Page> GetPageList(List <Paragraph> paragraphList, int pageWidth, int pageHeight, RssMedia rssMedia) { List <Page> pageList = new List <Page>(); Page page = new Page(); Line line = new Line(); Block block = new Block(0, 0, 0, 0); if (rssMedia.DisplayType == RssMedia.RssDisplayType.ScrollLeftToRight || rssMedia.DisplayType == RssMedia.RssDisplayType.ScrollRightToLeft) { foreach (Paragraph paragraph in paragraphList) { if (block.content == null) { line = new Line(); block = new Block(0, pageHeight / 2 - paragraph.font.Height, 0, pageHeight / 2); block.type = paragraph.type; block.font = paragraph.font; } else { line.Add(block); block = new Block(block.right, pageHeight / 2 - paragraph.font.Height, block.right, pageHeight / 2); block.type = paragraph.type; block.font = paragraph.font; } float characterWidth = 0.0f; foreach (Character character in paragraph.list) { characterWidth = GetCharacterWidth(character); //该行还能容下该字符 if (block.right + characterWidth < pageWidth) { block.Add(character, characterWidth); } //该行容不下该字符 else { if (block.content != null) { line.Add(block); page.Add(line); pageList.Add(page); page = new Page(); line = new Line(); block = new Block(0, pageHeight / 2 - paragraph.font.Height, 0, pageHeight / 2, paragraph.type); block.font = character.font; block.Add(character, characterWidth); } } } } line.Add(block); page.Add(line); pageList.Add(page); } else { foreach (Paragraph paragraph in paragraphList) { if (block.content != null) { line.Add(block); page.Add(line); } if (block.bottom + paragraph.font.Size * 4 / 3 < pageHeight) { block = new Block(0, block.bottom, 0, block.bottom + paragraph.font.Size * 4 / 3, paragraph.type); line = new Line(); } else { line.Add(block); pageList.Add(page); block = new Block(0, 0, 0, 0, paragraph.type); line = new Line(); page = new Page(); } block.font = paragraph.font; float characterWidth = 0.0f; foreach (Character character in paragraph.list) { characterWidth = GetCharacterWidth(character); //该行还能容下该字符 if (block.right + characterWidth < pageWidth) { block.font = character.font; block.Add(character, characterWidth); } //该行容不下该字符 else { //该页能够容下该行 if (block.bottom + character.height < pageHeight) { line.Add(block); page.Add(line); block = new Block(0, block.bottom, 0, block.bottom + character.height, paragraph.type); block.font = character.font; block.Add(character, characterWidth); line = new Line(); } //该页容不下该行 else { line.Add(block); page.Add(line); pageList.Add(page); page = new Page(); line = new Line(); block = new Block(0, 0, 0, 0, paragraph.type); block.font = character.font; block.Add(character, characterWidth); } } } } line.Add(block); page.Add(line); pageList.Add(page); } return(pageList); }