コード例 #1
1
        /// <summary>
        /// Convert string to ImageFormat
        /// </summary>
        /// <param name="format">Image format name</param>
        /// <example>
        /// <code>GetImageFormat("bmp")</code>
        /// </example>
        /// <returns>ImageFormat</returns>
        private static ImageFormat GetImageFormat(string format)
        {
            ImageFormat imageFormat = null;

            try
            {
                var imageFormatConverter = new ImageFormatConverter();
                imageFormat = (ImageFormat)imageFormatConverter.ConvertFromString(format);
            }
            catch (Exception e)
            {

                throw new FormatException();
            }

            return imageFormat;
        }
コード例 #2
0
		public void SetUp ()		
		{
			imageFmt = ImageFormat.Bmp; 
			imageFmtStr = imageFmt.ToString ();
		
			imgFmtConv = new ImageFormatConverter();
			imgFmtConvFrmTD = (ImageFormatConverter) TypeDescriptor.GetConverter (imageFmt);			
		}
コード例 #3
0
ファイル: HtmlHelper.cs プロジェクト: markglibres/cad.web
        //http://www.dailycoding.com/posts/convert_image_to_base64_string_and_base64_string_to_image.aspx
        public static string ConvertImageFileToEmbedded(string imageUrl)
        {
            string embeddedFormat = "data:{0};base64,{1}"; //contentType, base64 format
            string contentTypeFormat = "image/{0}";
            string contentType = "image/{0}";
            string imageBase64 = String.Empty;

            var converter = new ImageFormatConverter();
            
            using (MemoryStream stream = new MemoryStream())
            {
                Image image = Image.FromFile(imageUrl);
                image.Save(stream, image.RawFormat);
                byte[] imageBytes = stream.ToArray();
                imageBase64 = Convert.ToBase64String(imageBytes);
                contentType = String.Format(contentTypeFormat, converter.ConvertToString(image.RawFormat).ToLower());
            }

            string convertedSrc = String.Format(embeddedFormat, contentType, imageBase64);

            return convertedSrc;
        }
コード例 #4
0
        public ProfileModule(PeopleContext people)
        {
            this.RequiresAuthentication();

            _people = people;

            Get["/profile"] = parameters =>
            {
                // call when user visit it's own profile
                var identity = Context.CurrentUser as FloreamIdentity;
                var user = _people.People.FirstOrDefault(p => p.AdUser == identity.UserName);

                return View["profile", user];
            };

            Post["/profile/upload"] = parameters =>
            {
                var file = Request.Files.FirstOrDefault();
                if (file == null)
                {
                    return new Response().WithStatusCode(HttpStatusCode.BadRequest);
                }
                
                var identity = Context.CurrentUser as FloreamIdentity;
                var imageType = file.ContentType.Split('/')[1];
                var imageHeight = int.Parse(ConfigurationManager.AppSettings.Get("profile-image-height"));
                var imageWidth = int.Parse(ConfigurationManager.AppSettings.Get("profile-image-width"));

                var memStream = new MemoryStream();
                var img = Image.FromStream(file.Value);
                if (img.Height > imageHeight || img.Width > imageWidth)
                {
                    // Resize the image
                    var bmp = ScaleImage(img, imageWidth, imageHeight);
                    // Save the resized image to a stream
                    var imageFormatConverter = new ImageFormatConverter();
                    var imageObj = imageFormatConverter.ConvertFromString(imageType);
                    if (imageObj != null)
                    {
                        bmp.Save(memStream, (ImageFormat) imageObj);
                    }
                }
                else
                {
                    img.Save(memStream, img.RawFormat);
                }
                     
                var array = memStream.ToArray();

                // Update the user's profile
                var user = _people.People.FirstOrDefault(p => p.AdUser == identity.UserName);
                if (user != null)
                {
                    user.PictureExtension = imageType;
                    user.Picture = array;
                    _people.SaveChanges();
                }

                return Response.AsText(HtmlHelper.GetProfileImage(array, imageType));
            };
        }