Пример #1
0
 public void CropAndResizeImage(Image image, string outPutFilePath, string outPuthFileName, int?width = null, int?height = null, bool pngFormat = false)
 {
     try
     {
         if (!width.HasValue)
         {
             width = new int?(image.Width);
         }
         if (!height.HasValue)
         {
             height = new int?(image.Height);
         }
         KalikoImage kalikoImage  = new KalikoImage(image);
         KalikoImage kalikoImage1 = kalikoImage.Scale(new FitScaling(width.Value, height.Value));
         if (!Directory.Exists(HttpContext.Current.Server.MapPath(string.Concat("~/", outPutFilePath))))
         {
             Directory.CreateDirectory(HttpContext.Current.Server.MapPath(string.Concat("~/", outPutFilePath)));
         }
         string str = HttpContext.Current.Server.MapPath(string.Concat("~/", Path.Combine(outPutFilePath, outPuthFileName)));
         if (!pngFormat)
         {
             kalikoImage1.SaveJpg(str, (long)99);
         }
         else
         {
             kalikoImage1.SavePng(str);
         }
         kalikoImage1.Dispose();
         kalikoImage.Dispose();
     }
     catch (Exception exception)
     {
         throw new Exception(exception.Message);
     }
 }
Пример #2
0
        private static BitmapImage KalikoToBitmap(KalikoImage kimage)
        {
            var stream = new MemoryStream();

            kimage.SavePng(stream);
            stream.Position = 0;
            var bitmap = new BitmapImage();

            bitmap.BeginInit();
            bitmap.CacheOption  = BitmapCacheOption.OnLoad;
            bitmap.StreamSource = stream;
            bitmap.EndInit();
            bitmap.Freeze();
            return(bitmap);
        }
Пример #3
0
        private BitmapImage ImageToBitmapImage(KalikoImage image)
        {
            using (var memory = new MemoryStream())
            {
                image.SavePng(memory, true);
                memory.Position = 0;

                var bitmapImage = new BitmapImage();
                bitmapImage.BeginInit();
                bitmapImage.StreamSource = memory;
                bitmapImage.CacheOption  = BitmapCacheOption.OnLoad;
                bitmapImage.EndInit();

                return(bitmapImage);
            }
        }
Пример #4
0
        public void CropAndResizeImage(HttpPostedFileBase imageFile, string outPutFilePath, string outPuthFileName, int width, int height, bool pngFormat = false)
        {
            try
            {
                Image image = Image.FromStream(imageFile.InputStream);
                //if (!width.HasValue)
                //{
                //	width = image.Width;
                //}
                //if (!height.HasValue)
                //{
                //	height = new int?(image.Height);
                //}
                KalikoImage kalikoImage = new KalikoImage(image);

                kalikoImage.Resize(width, height);

                //KalikoImage kalikoImage1 = kalikoImage.Scale(new CropScaling(width, height));
                //KalikoImage kalikoImage1 = kalikoImage.Scale(new FitScaling(width.Value, height.Value));
                if (!Directory.Exists(HttpContext.Current.Server.MapPath(string.Concat("~/", outPutFilePath))))
                {
                    Directory.CreateDirectory(HttpContext.Current.Server.MapPath(string.Concat("~/", outPutFilePath)));
                }
                string str = HttpContext.Current.Server.MapPath(string.Concat("~/", Path.Combine(outPutFilePath, outPuthFileName)));
                if (!pngFormat)
                {
                    kalikoImage.SaveJpg(str, 99);
                }
                else
                {
                    kalikoImage.SavePng(str);
                }
                kalikoImage.Dispose();
                //kalikoImage.Dispose();
            }
            catch (Exception exception)
            {
                throw new Exception(exception.Message);
            }
        }
Пример #5
0
        /// <summary>
        /// 切割图片
        /// </summary>
        /// <param name="pageCount">切成几张</param>
        /// <returns></returns>
        private List <string> kalikoImage(int pageCount)
        {
            List <string> imageList = new List <string>();

            for (var n = 0; n < pageCount; n++)
            {
                KalikoImage ki = new KalikoImage(SealPath);
                var         w  = ki.Width / pageCount; // 计算每张图片的宽度

                ki.Crop(w * n, 0, w, ki.Height);
                //string path = System.IO.Path.Combine(config.Value.Substring(0, config.Value.LastIndexOf(".")), string.Format("{0}{1}.png", count, n));
                //if (!System.IO.Directory.Exists(config.Value.Substring(0, config.Value.LastIndexOf("."))))
                //{
                //    System.IO.Directory.CreateDirectory(config.Value.Substring(0, config.Value.LastIndexOf(".")));//不存在就创建目录
                //}
                string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Temp", string.Format("{0}{1}.png", pageCount, n));
                ki.SavePng(path);
                ki.Dispose();
                imageList.Add(path);
            }
            return(imageList);
        }
Пример #6
0
            public uint Def_ReadFile(string filename, IntPtr buffer, uint BufferSize, ref uint NumberByteReadSuccess, long Offset, IntPtr info)
            {
                StringBuilder data  = new StringBuilder();
                VNode         Node  = new VNode(filename);
                string        query = "";

                byte[] file = null;

                Console.WriteLine("reading {0} {1} {2}", Node.isValid, Node.fileName, Node.curDir);
                if (Node.isValid && Node.param.TryGetValue("q", out query))
                {
                    #region This Is google Service
                    if (Node.fileName.StartsWith("google", StringComparison.CurrentCultureIgnoreCase))
                    {
                        Task <Google.Apis.Customsearch.v1.Data.Search> ret;
                        if (Node.param.ContainsKey("n"))
                        {
                            int    n    = 1;
                            string temp = "";
                            Node.param.TryGetValue("n", out temp);
                            int.TryParse(temp, out n);
                            ret = ServiceProvider.SearchGoogle(query, n);
                        }
                        else
                        {
                            ret = ServiceProvider.SearchGoogle(query, 1);
                        }
                        ret.Wait();
                        if (ret.Result.Items != null)
                        {
                            foreach (Google.Apis.Customsearch.v1.Data.Result s in ret.Result.Items)
                            {
                                data.AppendLine(s.Title);
                                data.AppendLine("----------------------------------------------------------------");
                                data.AppendLine(s.Snippet);
                                data.AppendLine(s.Link);
                                data.Append("\n");
                            }
                        }
                        Console.WriteLine(data.ToString());
                        file = System.Text.Encoding.ASCII.GetBytes(data.ToString());
                    }
                    #endregion
                }
                else if (Node.curDir == "ImagePass")
                {
                    if (!Node.fileExtention.EndsWith("inf", StringComparison.CurrentCultureIgnoreCase) && !Node.fileExtention.EndsWith("ini", StringComparison.CurrentCultureIgnoreCase))
                    {
                        if (imageFiles.Any(name => name.StartsWith(Node.fileName)))
                        {
                            Console.WriteLine("Direct Read");
                            KalikoImage image = new KalikoImage(filepath + @"\" + imageFiles.Where(d => d.StartsWith(Node.fileName)).ToList()[0]);
                            Console.WriteLine("HEHFKHLDSKHFLSDHLKFHKD *()&*&(&**&(" + image.ByteArray.Length);
                            string width = "", height = "", OP = "", x = "", y = "";
                            System.IO.MemoryStream ms = new System.IO.MemoryStream();

                            Node.param.TryGetValue("op", out OP);
                            if (OP == "s" && Node.param.TryGetValue("w", out width) && Node.param.TryGetValue("h", out width))
                            {
                                int w = 0, h = 0;
                                if (int.TryParse(width, out w) && int.TryParse(width, out h))
                                {
                                    image.Scale(new FitScaling(w, h)).SavePng(ms);
                                }
                            }
                            if (OP == "c" && Node.param.TryGetValue("w", out width) && Node.param.TryGetValue("h", out width) && Node.param.TryGetValue("x", out x) && Node.param.TryGetValue("y", out y))
                            {
                                int w = 0, h = 0, X, Y;

                                if (int.TryParse(width, out w) && int.TryParse(width, out h) && int.TryParse(x, out X) && int.TryParse(y, out Y))
                                {
                                    image.Crop(X, Y, w, h);
                                }
                                image.SavePng(ms);
                            }

                            string ext = Node.fileExtention.ToLowerInvariant();


                            switch (ext)
                            {
                            case "png":
                                image.LoadImage(ms);
                                image.SavePng(ms);

                                break;

                            case "gif":
                                image.LoadImage(ms);
                                image.SaveGif(ms);
                                break;

                            case "bmp":
                                image.LoadImage(ms);
                                image.SaveBmp(ms);
                                break;

                            default:
                                image.LoadImage(ms);
                                image.SaveJpg(ms, 100);
                                break;
                            }
                            file = ms.ToArray();
                        }
                    }
                }


                if (file != null && file.Length != 0 && Offset < file.Length)
                {
                    if (BufferSize > file.Length - Offset)
                    {
                        NumberByteReadSuccess = (uint)(file.Length - Offset);
                        System.Runtime.InteropServices.Marshal.Copy(file, (int)Offset, buffer, (int)NumberByteReadSuccess);
                    }
                    else
                    {
                        NumberByteReadSuccess = BufferSize;
                        System.Runtime.InteropServices.Marshal.Copy(file, (int)Offset, buffer, (int)BufferSize);
                    }
                    return(0);
                }
                else
                {
                    Console.WriteLine("Error param {0}", Node.fileName);
                    return(0xC000000F);
                }
            }
Пример #7
0
        /// <summary>
        /// Tries to handle an incoming request.
        /// </summary>
        /// <param name="api">The current api</param>
        /// <param name="request">The incoming route request</param>
        /// <returns>The result</returns>
        public IResponse Handle(Api api, IRequest request)
        {
            var slug = request.Segments.Length > 1 ? request.Segments[1] : "";
            int?width = null, height = null;

            if (!String.IsNullOrWhiteSpace(slug))
            {
                var index = slug.LastIndexOf('.');

                if (index != -1)
                {
                    var name   = slug.Substring(0, index);
                    var ending = slug.Substring(index);

                    var segments = name.Split(new char[] { '_' });

                    if (segments.Length > 2)
                    {
                        height = Convert.ToInt32(segments[2]);
                    }
                    if (segments.Length > 1)
                    {
                        width = Convert.ToInt32(segments[1]);
                    }
                    slug = segments[0] + ending;
                }

                var media = api.Media.GetSingle(slug);

                if (media != null)
                {
                    var response = request.StreamResponse();
                    var data     = App.Media.Get(media);

                    if (data != null)
                    {
                        response.ContentType = media.ContentType;

                        if (width.HasValue)
                        {
                            using (var mem = new MemoryStream(data)) {
                                var image = new KalikoImage(mem);
                                var scale = height.HasValue ?
                                            (ScalingBase) new CropScaling(width.Value, height.Value) :
                                            (ScalingBase) new FitScaling(width.Value, Int32.MaxValue);

                                image = image.Scale(scale);
                                image.SavePng(response.OutputStream);
                            }
                        }
                        else
                        {
                            using (var writer = new BinaryWriter(response.OutputStream)) {
                                writer.Write(data);
                            }
                        }
                        return(response);
                    }
                }
            }
            return(null);
        }