Esempio n. 1
0
        //public ImagePathInfo GetImageForWindowControl(Ctx ctx, int ad_image_id, int height, int width)
        //{
        //    MImage mimg = new MImage(ctx, ad_image_id, null);
        //    if (!string.IsNullOrEmpty(mimg.GetFontName()))
        //    {
        //        return new ImagePathInfo() { ClassName = mimg.GetFontName() };
        //    }
        //    var value = mimg.GetThumbnailByte(height, width);
        //    if (value != null)
        //    {
        //        UsrImage = Convert.ToBase64String(value);
        //        //obj.UsrImage = Convert.ToBase64String(mimg.GetBinaryData());
        //        if (mimg.GetBinaryData() != null)
        //        {
        //            Isdatabase = true;
        //        }
        //        return new ImagePathInfo() { Path = UsrImage };
        //    }
        //    return null;
        //}

        /// <summary>
        /// Save images
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="serverPath"></param>
        /// <param name="file"></param>
        /// <param name="imageID"></param>
        /// <param name="isDatabaseSave"></param>
        /// <returns></returns>
        public int SaveImage(Ctx ctx, string serverPath, HttpPostedFileBase file, int imageID, bool isDatabaseSave)
        {
            // check if its new insert or upadate, true in case if upload file has value
            MemoryStream ms = null;

            try
            {
                if (file != null)
                {
                    HttpPostedFileBase hpf = file as HttpPostedFileBase;

                    string savedFileName = Path.Combine(serverPath, Path.GetFileName(hpf.FileName));
                    hpf.SaveAs(savedFileName);
                    ms = new MemoryStream();
                    hpf.InputStream.CopyTo(ms);
                    byte[]   byteArray = ms.ToArray();
                    FileInfo file1     = new FileInfo(savedFileName);
                    if (file1.Exists)
                    {
                        file1.Delete(); //Delete Temporary file
                    }
                    ms.Dispose();
                    ms = null;
                    string imgByte = Convert.ToBase64String(byteArray);
                    var    id      = CommonFunctions.SaveImage(ctx, byteArray, imageID, hpf.FileName.Substring(hpf.FileName.LastIndexOf('.')), isDatabaseSave);
                    return(id);
                }
                else
                {
                    // update database with image blob, this occurs when save in db check box changed
                    if (imageID > 0)
                    {
                        string imageUrl = new MImage(ctx, imageID, null).GetImageURL();
                        if (imageUrl != null)
                        {
                            string imageName         = imageUrl.Substring(imageUrl.LastIndexOf('/') + 1);
                            System.Drawing.Image img = System.Drawing.Image.FromFile(Path.Combine(HostingEnvironment.MapPath(@"~/Images"), imageName));
                            ms = new MemoryStream();
                            img.Save(ms, img.RawFormat);
                            var id = CommonFunctions.SaveImage(ctx, ms.ToArray(), imageID, imageName, isDatabaseSave);
                            ms.Dispose();
                            ms = null;
                            return(id);
                        }
                        else
                        {
                            return(0);
                        }
                    }
                    else
                    {
                        return(0);
                    }
                }
            }
            finally
            {
                if (ms != null)
                {
                    ms.Dispose();
                    ms = null;
                }
            }
        }