コード例 #1
0
ファイル: frmRssEditor.cs プロジェクト: shouzhuo1/MOOC_CSharp
        //点击 “确定”按钮
        private void btnOK_Click(object sender, EventArgs e)
        {
            string title      = txtTitle.Text.Trim();
            string url        = txtUrl.Text.Trim();
            string desciption = txtDescription.Text.Trim();
            int    category   = Convert.ToInt32(Tag);

            var newRss = new RssInfo()
            {
                Title = title, Description = desciption, Url = url, Category = category
            };


            if (_rss == null)
            {
                //添加新的 RSS
                RssDAL.Add(newRss);
            }
            else
            {
                //修改已有 RSS
                RssDAL.Update(_rss.ID, newRss);
            }
            OnDataChanged();
            Close();
        }
コード例 #2
0
        //添加新的 RSS 信息
        public static void Add(RssInfo rssInfo)
        {
            string commandText = $"INSERT INTO [rss] (Title, Description, Url, Category) " +
                                 $"VALUES ('{rssInfo.Title}', '{rssInfo.Description}','{rssInfo.Url}', {rssInfo.Category})";

            DBHelper.ExecuteNonQuery(commandText);
        }
コード例 #3
0
ファイル: frmRssEditor.cs プロジェクト: shouzhuo1/MOOC_CSharp
 //通过构造函数传入要编辑的 RSS 信息
 public frmRssEditor(RssInfo rss)
 {
     InitializeComponent();
     if (rss != null)
     {
         _rss = rss;
     }
 }
コード例 #4
0
        //修改指定的 RSS 信息
        public static void Update(int id, RssInfo rssInfo)
        {
            string commandText = $"UPDATE [rss] " +
                                 $"SET Title = '{rssInfo.Title}', " +
                                 $"Description = '{rssInfo.Description}', " +
                                 $"Url = '{rssInfo.Url}' " +
                                 $"WHERE ID = {id}";

            DBHelper.ExecuteNonQuery(commandText);
        }
コード例 #5
0
        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);
            }
        }
コード例 #6
0
        /// <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);
        }
コード例 #7
0
        public void RssInfoTest()
        {
            RssInfo info = new RssInfo {
                Channel = new RssChannelInfo {
                    Title       = "首页",
                    Link        = "http://www.xphter.com",
                    Description = "科锐软件工作室",
                    Categories  = new List <RssCategoryInfo> {
                        new RssCategoryInfo {
                            Domain = "syndic8",
                            Value  = "IT",
                        },
                        new RssCategoryInfo {
                            Domain = "syndic8",
                            Value  = "News",
                        },
                    },
                    Copyright = "xphter.com 2014",
                    Docs      = "http://www.xphter.com/rss",
                    Generator = "Xphter SiteBuilder",
                    Ttl       = 60,
                    Image     = new RssImageInfo {
                        Url         = "http://www.xphter.com/logo.png",
                        Title       = "首页",
                        Link        = "http://www.xphter.com",
                        Description = "科锐软件工作室",
                        Width       = 100,
                        Height      = 120,
                    },
                    Language       = "zh-cn",
                    LastBuildDate  = DateTime.Today,
                    PubDate        = DateTime.Today,
                    ManagingEditor = "*****@*****.**",
                    WebMaster      = "*****@*****.**",
                    Items          = new List <RssItemInfo> {
                        new RssItemInfo {
                            Title       = "新闻1",
                            Link        = "http://www.xphter.com/news/1.html",
                            Description = "新闻1描述",
                            PubDate     = DateTime.Today,
                            Guid        = "1",
                            Author      = "*****@*****.**",
                            Source      = "新闻1来源",
                            Categories  = new List <RssCategoryInfo> {
                                new RssCategoryInfo {
                                    Domain = "syndic8",
                                    Value  = "IT",
                                },
                                new RssCategoryInfo {
                                    Domain = "syndic8",
                                    Value  = "News",
                                },
                            },
                            Comments  = "新闻1备注",
                            Enclosure = new RssEnclosureInfo {
                                Url    = "http://www.xphter.com/news/1.jpg",
                                Type   = "image/jepg",
                                Length = 100,
                            },
                        },
                        new RssItemInfo {
                            Title       = "新闻2",
                            Link        = "http://www.xphter.com/news/2.html",
                            Description = "新闻2描述",
                            PubDate     = DateTime.Today,
                            Guid        = "2",
                            Author      = "*****@*****.**",
                            Source      = "新闻2来源",
                            Categories  = new List <RssCategoryInfo> {
                                new RssCategoryInfo {
                                    Domain = "syndic8",
                                    Value  = "IT",
                                },
                                new RssCategoryInfo {
                                    Domain = "syndic8",
                                    Value  = "News",
                                },
                            },
                            Comments  = "新闻2备注",
                            Enclosure = new RssEnclosureInfo {
                                Url    = "http://www.xphter.com/news/2.jpg",
                                Type   = "image/jepg",
                                Length = 200,
                            },
                        },
                    },
                },
            };

            XmlSerializer serializer = new XmlSerializer(typeof(RssInfo));

            using (StreamWriter writer = new StreamWriter("rss.xml", false, Encoding.UTF8)) {
                serializer.Serialize(writer, info);
            }
        }
コード例 #8
0
 public ImageObj(RssInfo rssInfo, RssMedia rssMedia)
 {
     this.rssInfo  = rssInfo;
     this.rssMedia = rssMedia;
 }
コード例 #9
0
        /// <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;
            }
        }
コード例 #10
0
ファイル: MainWindow.xaml.cs プロジェクト: junsak77/rssreader
        public MainWindow()
        {
            InitializeComponent();

            try
            {
                // 取得するRSSのURL
                string rss = "https://news.yahoo.co.jp/rss/topics/it.xml";

                //XElementのLoadメソッドにURLを渡して情報を取得する
                //XElementのリファレンス:https://docs.microsoft.com/ja-jp/dotnet/api/system.xml.linq.xelement?view=net-5.0
                XElement element = XElement.Load(rss);

                //"channel"エレメントを取得
                XElement channelElement = element.Element("channel");

                //RssInfoクラスのインスタンスを生成
                RssInfo rssInfo = new RssInfo();
                //ここではヘッダ情報を取得している
                //それぞれのメンバ変数へ取得したchannelエレメントの中から指定したエレメントの中身を取得
                rssInfo.Title       = channelElement.Element("title").Value;
                rssInfo.Description = channelElement.Element("description").Value;
                rssInfo.Link        = channelElement.Element("link").Value;
                rssInfo.PubDate     = channelElement.Element("pubDate").Value;

                //Itemエレメントを全て取得する
                //<Item>
                //  <item1>
                //</Item>
                //<Item>
                //  <item2>
                //</Item>
                //<item1><item2>が取得される
                IEnumerable <XElement> elementItems = channelElement.Elements("item");

                //ListをRssItemInfoクラス型で作成する
                List <RssItemInfo> rssItemInfos = new List <RssItemInfo>();
                //elementItemsの中身を一つずつ取得
                foreach (XElement elmItem in elementItems)
                {
                    RssItemInfo itemInfo = new RssItemInfo();
                    //それぞれのElementに含まれる値を取得してくる
                    itemInfo.Title       = elmItem.Element("title").Value;
                    itemInfo.Description = elmItem.Element("description").Value;
                    itemInfo.Link        = elmItem.Element("link").Value;
                    itemInfo.PubDate     = elmItem.Element("pubDate").Value;
                    rssItemInfos.Add(itemInfo);
                }

                //タイトルを取得して文字列型に格納
                string rsstext = "";
                foreach (var work in rssItemInfos)
                {
                    //タイトルを文字列結合してTextBlockへ反映
                    rsstext += work.Title + "\n";
                    //ボタンの拡張クラスを作って押すと別ウィンドウに情報を出すようにする
                    CustomButton bt = new CustomButton();
                    bt.Content  = work.Title;
                    bt.rii      = work;
                    bt.Height   = 100;
                    bt.FontSize = 36;
                    //クリックイベントのイベントハンドラを設定
                    bt.Click += new RoutedEventHandler(CustomButton_Click);
                    stack.Children.Add(bt);
                }
                //textblock.Text = rsstext;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
            }
        }