示例#1
0
 public RenderListener(PdfReader reader, int pageNumber, float pageUnits)
 {
     _reader     = reader;
     _pageNumber = pageNumber;
     _pageUnits  = pageUnits;
     ImagesInfo  = new ImagesInfo();
     TextsInfo   = new TextsInfo();
 }
示例#2
0
    public void RenderImage(ImageRenderInfo renderInfo)
    {
        float  area   = renderInfo.GetArea();
        Matrix matrix = renderInfo.GetImageCTM();

        int?mcid = renderInfo.GetMcid();

        PdfIndirectReference indirectRef = renderInfo.GetRef();

        if (indirectRef == null)
        {
            return;
        }

        PdfObject     pdfObject     = null;
        PdfDictionary imgDictionary = null;
        ImageInfo     imageInfo     = null;



        PdfObject     directObj = _reader.GetPdfObject(indirectRef.Number);
        PdfDictionary imgObject = (PdfDictionary)PdfReader.GetPdfObject(directObj);

        imageInfo = new ImageInfo(imgObject, matrix, _pageUnits, _pageNumber, renderInfo);

        if (imageInfo.IsImage)
        {
            imgObject.Put(new PdfName("ITXT_ObjectId"), new PdfName(imageInfo.ID.ToString()));
            ImagesInfo.Add(imageInfo);
        }

        //PRStream maskStream = (PRStream)imgDictionary.GetAsStream(PdfName.MASK) ?? (PRStream)imgDictionary.GetAsStream(PdfName.SMASK);
        //if (maskStream != null)
        //{
        //	PdfImageObject maskImage = new PdfImageObject(maskStream);
        //	string filtype = maskImage.GetFileType();
        //	var bytes = maskImage.GetImageAsBytes();
        //	System.IO.File.WriteAllBytes(@"c:\1.png", bytes);
        //}
        //var props = renderInfo.GetImage().GetDrawingImage();

        //if (extension == ".jpeg")
        //	tempImage.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
        //else

        //props.Save(@"c:\1.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
        //if (indirectRef != null)
        //{
        //	pdfObject = _reader.GetPdfObject(indirectRef.Number);
        //	imgDictionary = (PdfDictionary) PdfReader.GetPdfObject(pdfObject);
        //	imageInfo = new ImageInfo(imgDictionary, matrix, _pageUnits, _pageNumber, renderInfo);
        //}
        //else
        //{
        //	imgDictionary = renderInfo.GetImage().GetDictionary();
        //	imageInfo = new ImageInfo(imgDictionary, matrix, _pageUnits, _pageNumber, renderInfo);
        //}
    }
示例#3
0
        // GET api/<controller>
        /// <summary>
        /// GET
        /// </summary>
        /// <returns>戻り値</returns>
        public HttpResponseMessage Get()
        //public ImagesInfo Get()
        {
            //画像のパスを取得
            var rootPath = HttpContext.Current.Server.MapPath("./");

            //画像データの取得
            var imgs = new ImagesInfo();

            imgs.Data.Add(ReadImageData(System.IO.Path.Combine(rootPath, @"images\test.png")));
            imgs.Data.Add(ReadImageData(System.IO.Path.Combine(rootPath, @"images\test2.png")));
            imgs.Number = imgs.Data.Count;

            //レスポンスの作成
            var res = new HttpResponseMessage();

            res.StatusCode = HttpStatusCode.OK;

            //TODO もっといい方法がないか?共通化できないか?
            //レスポンスをjsonかxmlどちらにするかを決定
            if (HttpContext.Current.Request.ContentType == "application/xml")
            {
                //レスポンス:XML
                using (var ms = new MemoryStream())
                {
                    var ser = new XmlSerializer(typeof(ImagesInfo));
                    ser.Serialize(ms, imgs);
                    res.Content = new StringContent(Encoding.UTF8.GetString(ms.ToArray()), Encoding.UTF8, "application/xml");
                }
            }
            else
            {
                //レスポンス:JSON
                res.Content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(imgs), Encoding.UTF8, "application/json");
            }

            return(res);
        }
        public JsonResult UploadImages()
        {
            try
            {
                string resultString = "Files are seccessfully uploaded";
                string resultType   = "success";

                Repository <ImagesInfo> imageInfoRepository = new Repository <ImagesInfo>();

                ImagesInfo imgInfo = imageInfoRepository.GetList().First();

                Repository <ImageEntity> imageRepository = new Repository <ImageEntity>();

                foreach (string file in Request.Files)
                {
                    imgInfo.TotalCount++;
                    var    upload   = Request.Files[file];
                    string fileName = "Image" + imgInfo.TotalCount + upload.FileName.Substring(upload.FileName.LastIndexOf('.'));

                    if (upload != null)
                    {
                        string foundTags  = String.Empty;
                        string resultTags = String.Empty;
                        using (Image inputImage = Image.FromStream(upload.InputStream))
                        {
                            try
                            {
                                PropertyItem basicTag = inputImage.GetPropertyItem(40094); // Hex 9C9E
                                if (basicTag != null)
                                {
                                    foundTags = Encoding.Unicode.GetString(basicTag.Value).Replace("\0", string.Empty);
                                    Char[]   separators = { ',', ';', ' ' };
                                    string[] tagsList   = DeleteLastSemicolon(foundTags.Trim()).Split(separators);
                                    for (int i = 0; i < tagsList.Length; i++)
                                    {
                                        if (tagsList[i] == "")
                                        {
                                            continue;
                                        }
                                        string tag = tagsList[i].Trim().ToLower();
                                        resultTags += tag;
                                        resultTags += ";";
                                    }
                                }
                            }
                            // ArgumentException is thrown when GetPropertyItem(int) is not found
                            catch (ArgumentException)
                            {
                                resultString = "No data in property \"Tag\" in some image or Property \"Tags\" isn't found in this type of file";
                                resultType   = "danger";
                            }
                        }

                        string imagePath = Server.MapPath("~/Content/Images/Images/LargeImages/Temp" + fileName);
                        upload.SaveAs(imagePath);
                        imageInfoRepository.Update(imgInfo);

                        //FileInfo savedImage = new FileInfo(Server.MapPath("~/Content/Images/Images/LargeImages/" + fileName));
                        double height;
                        double width;
                        double proportion;
                        double newHeight;
                        int    newWidth;


                        using (Image imageFromPath = Image.FromFile(imagePath))
                        {
                            height     = imageFromPath.Height;
                            width      = imageFromPath.Width;
                            proportion = height / width;
                            newHeight  = 2000;
                            newWidth   = (int)(newHeight / proportion);
                            if (newWidth > 5120)
                            {
                                newWidth  = 5120;
                                newHeight = (int)(newWidth * proportion);
                            }
                            Bitmap resizedImage = changeMainImageSize(imagePath, (int)newHeight, newWidth);

                            if (resizedImage != null)
                            {
                                resizedImage.Save(Server.MapPath("~/Content/Images/Images/LargeImages/") + fileName);
                            }
                        }

                        using (Image imageFromPath = Image.FromFile(imagePath))
                        {
                            newHeight = 600;
                            newWidth  = (int)(newHeight / proportion);
                            if (newWidth > 5120)
                            {
                                newWidth  = 5120;
                                newHeight = (int)(newWidth * proportion);
                            }
                            Bitmap resizedImage = changeMainImageSize(imagePath, (int)newHeight, newWidth);
                            if (resizedImage != null)
                            {
                                resizedImage.Save(Server.MapPath("~/Content/Images/Images/MiddleImages/") + fileName);
                            }
                        }

                        using (Image imageFromPath = Image.FromFile(imagePath))
                        {
                            newHeight = 200;
                            newWidth  = (int)(newHeight / proportion);
                            if (newWidth > 5120)
                            {
                                newWidth  = 5120;
                                newHeight = (int)(newWidth * proportion);
                            }
                            Bitmap resizedImage = changeMainImageSize(imagePath, (int)newHeight, newWidth);
                            if (resizedImage != null)
                            {
                                resizedImage.Save(Server.MapPath("~/Content/Images/Images/SmallImages/") + fileName);
                            }
                        }

                        System.IO.File.Delete(imagePath);

                        ImageEntity image = new ImageEntity {
                            Name = fileName, Tags = resultTags != string.Empty ? DeleteLastSemicolon(resultTags) : null, AddingTime = DateTime.Now
                        };
                        imageRepository.Create(image);
                    }
                }

                return(Json(new { type = resultType, resultString = resultString }));
            }
            catch (Exception ex)
            {
                ExceptionProcessor exceptionProcessor = new ExceptionProcessor();
                exceptionProcessor.process(ex);
                return(Json("Exception"));
            }
        }
示例#5
0
        protected void btnCrop_OnClick(object sender, EventArgs e)
        {
            int X1 = Convert.ToInt32(Request.Form["x1"]);
            int Y1 = Convert.ToInt32(Request["y1"]);
            int X2 = Convert.ToInt32(Request.Form["x2"]);
            int Y2 = Convert.ToInt32(Request.Form["y2"]);
            int X  = System.Math.Min(X1, X2);
            int Y  = System.Math.Min(Y1, Y2);
            int w  = Convert.ToInt32(Request.Form["w"]);
            int h  = Convert.ToInt32(Request.Form["h"]);

            // That can be any image type (jpg,jpeg,png,gif) from any where in the local server
            bool   flag         = true;
            string originalFile = "";
            string oldFile      = "";

            try
            {
                originalFile = Server.MapPath(originalImage.Src);
                oldFile      = Server.MapPath(originalImage.Src);
            }
            catch (Exception ex)
            {
                flag         = false;
                originalFile = originalImage.Src;
            }
            var aImageInfos     = (List <ImagesInfo>)Session["AllImageLink"];
            var aImageInfosCrop = (List <ImagesInfo>)Session["AllImageLinkCrop"];


            System.Drawing.Image img = null;
            var aImageInfo           = new ImagesInfo();
            var aImageInfo1          = new ImagesInfo();

            if (flag)
            {
                img         = System.Drawing.Image.FromFile(originalFile);
                aImageInfo  = aImageInfos.Where(x => x.ImagesLink.Trim() == originalImage.Src).FirstOrDefault();
                aImageInfo1 = aImageInfosCrop.Where(x => x.ImagesLink.Trim() == originalImage.Src).FirstOrDefault();
            }
            else
            {
                img         = DownloadImageFromUrl(originalFile);
                aImageInfo  = aImageInfos.Where(x => x.ImagesLink.Trim() == originalFile).FirstOrDefault();
                aImageInfo1 = aImageInfosCrop.Where(x => x.ImagesLink.Trim() == originalFile).FirstOrDefault();
            }

            using (Bitmap _bitmap = new Bitmap(w, h))
            {
                _bitmap.SetResolution(img.HorizontalResolution, img.VerticalResolution);
                using (Graphics _graphic = Graphics.FromImage(_bitmap))
                {
                    _graphic.InterpolationMode  = InterpolationMode.HighQualityBicubic;
                    _graphic.SmoothingMode      = SmoothingMode.HighQuality;
                    _graphic.PixelOffsetMode    = PixelOffsetMode.HighQuality;
                    _graphic.CompositingQuality = CompositingQuality.HighQuality;
                    _graphic.DrawImage(img, 0, 0, w, h);
                    _graphic.DrawImage(img, new Rectangle(0, 0, w, h), X, Y, w, h, GraphicsUnit.Pixel);


                    if (txtAdditionalText.Value.Trim() != "")
                    {
                        // For Transparent Watermark Text
                        int opacity = 255; // range from 0 to 255

                        //SolidBrush brush = new SolidBrush(Color.Red);
                        SolidBrush brush = new SolidBrush(Color.FromArgb(opacity, Color.White));
                        Font       font  = new Font("Arial", 16);
                        _graphic.DrawString(txtAdditionalText.Value, font, brush, new PointF(0, 10));
                    }

                    string extension       = Path.GetExtension(originalFile);
                    string croppedFileName = Guid.NewGuid().ToString();
                    string path            = Server.MapPath("~/Upload/");


                    // If the image is a gif file, change it into png
                    if (extension.EndsWith("gif", StringComparison.OrdinalIgnoreCase))
                    {
                        extension = ".png";
                    }

                    string newFullPathName = string.Concat(path, croppedFileName, extension);

                    using (EncoderParameters encoderParameters = new EncoderParameters(1))
                    {
                        encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality,
                                                                          value: 100L);
                        _bitmap.Save(newFullPathName, GetImageCodec(extension), encoderParameters);
                    }
                    string filename = string.Concat("/Upload/", croppedFileName + extension);
                    aImageInfo.ImagesLink  = filename;
                    aImageInfo1.ImagesLink = filename;
                    originalImage.Src      = filename;
                    if (File.Exists(oldFile))
                    {
                        File.Delete(oldFile);
                    }
                }
            }
            Session["AllImageLinkCrop"] = aImageInfosCrop;
            Session["AllImageLink"]     = aImageInfos;
        }