Exemplo n.º 1
0
        public void PostImage([FromBody] uploadModel value)
        {
            for (int i = 0; i < value.Name.Length; i++)
            {
                var    media      = value.Name[i];
                byte[] imageBytes = Convert.FromBase64String(media);
                var    ms         = new MemoryStream(imageBytes, 0, imageBytes.Length);

                // xml document that will has all picture to save to DB.
                var        xmldoc = new XmlDocument();
                XmlElement doc    = xmldoc.CreateElement("doc");
                xmldoc.AppendChild(doc);

                // Convert byte[] to Image
                ms.Write(imageBytes, 0, imageBytes.Length);
                var image = Image.FromStream(ms, true);


                string newFile  = string.Format("{0}.jpg", Guid.NewGuid()),
                       path     = HostingEnvironment.MapPath("~/Public/images/"),
                       filePath = Path.Combine(path, newFile);

                // add this picture to list to save into DB.
                XmlElement xmlelement = xmldoc.CreateElement("Pictures");
                xmlelement.SetAttribute("MediaUrl", newFile);
                xmlelement.SetAttribute("PropertyID", value.ID);
                xmlelement.SetAttribute("Index", string.Format("{0}", i + 1));
                doc.AppendChild(xmlelement);

                try
                {
                    image.Save(filePath, ImageFormat.Jpeg);

                    // Save Thumb image //////////////////////////////////
                    // Set image height and width to be loaded on web page
                    byte[] buffer = getResizedImage(filePath, 150, 150);
                    // prepaire thumb folder
                    string pPath = Path.Combine(path, "_thumb\\");
                    if (!Directory.Exists(pPath))
                    {
                        Directory.CreateDirectory(pPath);
                    }
                    // save image in thumb folder
                    File.WriteAllBytes(pPath + newFile, buffer);
                    // end ///////////////////////////////////////////////


                    // start save all into db.
                    SaveDB(xmldoc.OuterXml);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
Exemplo n.º 2
0
        public IHttpActionResult PostPhoto([FromBody] uploadModel value)
        {
            if (string.IsNullOrEmpty(value.ID))
            {
                return(Content(HttpStatusCode.BadRequest, "Error!"));
            }

            byte[] imageBytes = Convert.FromBase64String(value.ID);
            var    ms         = new MemoryStream(imageBytes, 0, imageBytes.Length);

            // Convert byte[] to Image
            ms.Write(imageBytes, 0, imageBytes.Length);
            var image = Image.FromStream(ms, true);

            string newFile  = string.Format("{0}.jpg", Guid.NewGuid()),
                   path     = HostingEnvironment.MapPath("~/Public/images/news/"),
                   filePath = Path.Combine(path, newFile);

            try
            {
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                image.Save(filePath, ImageFormat.Jpeg);

                // Save Thumb image //////////////////////////////////
                // Set image height and width to be loaded on web page
                byte[] buffer = getResizedImage(filePath, 150, 150);
                // prepaire thumb folder
                string pPath = Path.Combine(path, "_thumb\\");
                if (!Directory.Exists(pPath))
                {
                    Directory.CreateDirectory(pPath);
                }
                // save image in thumb folder
                File.WriteAllBytes(pPath + newFile, buffer);
                // end ///////////////////////////////////////////////

                return(Ok(newFile));
            }
            catch (Exception ex)
            {
                return(Content(HttpStatusCode.BadRequest, "Error!" + ex.Message));
            }
        }