コード例 #1
0
ファイル: SyncCore.cs プロジェクト: ciker/WPYoudaonNote
        /// <summary>
        /// Download and save image to local.
        /// </summary>
        /// <param name="belongedNotePath">The path of belonged note.</param>
        /// <param name="imgNode">The image HtmlNode object.</param>
        /// <param name="imgRemoteUrl">The remote url of the image.</param>
        /// <param name="imgNameWithoutExt">The image name without extension.</param>
        /// <returns></returns>
        private string downloadAndSaveImageToLocal(string belongedNotePath, HtmlNode imgNode, string imgRemoteUrl, string imgNameWithoutExt)
        {
            Debug.WriteLine("Thread Id: " + Thread.CurrentThread.ManagedThreadId + " downloadAndSaveImageToLocal: " + imgRemoteUrl);
            var res = _api.DownloadAttach(imgRemoteUrl);

            var imgType   = ImageUtil.GetImageType(res.Buffer);
            var localPath = "Images\\" + imgNameWithoutExt + "." + imgType;

            double width;

            if (imgNode.Attributes.Contains("width"))
            {
                if (Double.TryParse(imgNode.Attributes["width"].Value, out width))
                {
                    if (width > _screenWidth)
                    {
                        width = _screenWidth - 25;
                    }
                }
                imgNode.Attributes["width"].Value = width.ToString(CultureInfo.InvariantCulture);
            }
            else
            {
                var actualSize = ImageUtil.GetImageSize(imgType, res.Buffer);
                width = actualSize.Width > _screenWidth ? _screenWidth - 25 : actualSize.Width;
                imgNode.Attributes.Add("width", width.ToString(CultureInfo.InvariantCulture));
            }
            // Save to disk.
            var savePath = IsoStoreUtil.SaveToIsoStore(localPath, res.Buffer);
            // Save to db.
            var imageEntity = new ImageEntity(imgNameWithoutExt, imgType.ToString(), savePath, imgRemoteUrl, belongedNotePath);

            ImageDao.InsertIfNotExist(imageEntity);

            return(savePath);
        }