Exemple #1
0
        public void TestLoadGIF89a()
        {
            GifDecoder a_decoder = new GifDecoder();
            Stream sm = Assembly.GetExecutingAssembly().GetManifestResourceStream("SnCore.Tools.Tests.Images.animated.gif");
            Console.WriteLine("Bytes: {0}", sm.Length);
            a_decoder.Read(sm);
            Console.WriteLine("Frames: {0}", a_decoder.GetFrameCount());

            sm.Seek(0, SeekOrigin.Begin);
            Assert.IsNotNull(sm);
            ThumbnailBitmap b = new ThumbnailBitmap(sm);
            Console.WriteLine("Size: {0}x{1}", b.FullSize.Width, b.FullSize.Height);
            Assert.AreEqual(new Size(320, 240), b.FullSize);
            Assert.IsNotNull(b.Thumbnail);
            Assert.IsNotNull(b.Bitmap);
            ThumbnailBitmap th = new ThumbnailBitmap(b.Thumbnail);
            Console.WriteLine("Size: {0}x{1}", th.FullSize.Width, th.FullSize.Height);
            Assert.AreEqual(new Size(150, 100), th.FullSize);
            
            // make sure that the bitmap is still animated            
            Console.WriteLine("Bytes: {0}", b.Bitmap.Length);
            GifDecoder decoder = new GifDecoder();
            decoder.Read(new MemoryStream(b.Bitmap));
            Console.WriteLine("Frames: {0}", decoder.GetFrameCount());

            GifDecoder th_decoder = new GifDecoder();
            Console.WriteLine("Bytes: {0}", b.Thumbnail.Length);
            th_decoder.Read(new MemoryStream(b.Thumbnail));
            Console.WriteLine("Frames: {0}", th_decoder.GetFrameCount());
            Assert.AreEqual(th_decoder.GetFrameCount(), decoder.GetFrameCount());
        }
Exemple #2
0
 public void TestLoadJPG()
 {
     Stream sm = Assembly.GetExecutingAssembly().GetManifestResourceStream("SnCore.Tools.Tests.Images.test.jpg");
     Assert.IsNotNull(sm);
     ThumbnailBitmap b = new ThumbnailBitmap(sm);
     Console.WriteLine("Size: {0}x{1}", b.FullSize.Width, b.FullSize.Height);
     Assert.AreEqual(ThumbnailBitmap.s_FullSize, b.FullSize);
     Assert.IsNotNull(b.Thumbnail);
     Assert.IsNotNull(b.Bitmap);
     ThumbnailBitmap th = new ThumbnailBitmap(b.Thumbnail);
     Console.WriteLine("Size: {0}x{1}", th.FullSize.Width, th.FullSize.Height);
     // horizontal bitmap, thumbnail size assumes vertical, thumbnail adjusted to horizontal
     Assert.AreEqual(new Size(ThumbnailBitmap.s_ThumbnailSize.Height, ThumbnailBitmap.s_ThumbnailSize.Width), th.FullSize);
 }
Exemple #3
0
        public void TestLoadAll()
        {
            string[] images = Assembly.GetExecutingAssembly().GetManifestResourceNames();
            foreach (string image in images)
            {
                if (!image.StartsWith("SnCore.Tools.Tests.Images."))
                    continue;

                Console.WriteLine(image);

                Stream sm = Assembly.GetExecutingAssembly().GetManifestResourceStream(image);
                Assert.IsNotNull(sm);
                ThumbnailBitmap b = new ThumbnailBitmap(sm, new Size(1, 1), ThumbnailBitmap.s_FullSize, ThumbnailBitmap.s_ThumbnailSize);
                Console.WriteLine("Size: {0}x{1}", b.FullSize.Width, b.FullSize.Height);
                Assert.IsNotNull(b.Bitmap);
            }
        }
    protected void files_FilesPosted(object sender, FilesPostedEventArgs e)
    {
        try
        {
            if (e.PostedFiles.Count == 0)
                return;

            ExceptionCollection exceptions = new ExceptionCollection();
            foreach (HttpPostedFile file in e.PostedFiles)
            {
                try
                {
                    TransitAccountPicture p = new TransitAccountPicture();

                    ThumbnailBitmap t = new ThumbnailBitmap(file.InputStream);
                    p.Bitmap = t.Bitmap;
                    p.Name = Path.GetFileName(file.FileName);
                    p.Description = string.Empty;
                    p.Hidden = false;

                    SessionManager.CreateOrUpdate<TransitAccountPicture>(
                        p, SessionManager.AccountService.CreateOrUpdateAccountPicture);
                }
                catch (Exception ex)
                {
                    exceptions.Add(new Exception(string.Format("Error processing {0}: {1}",
                        Renderer.Render(file.FileName), ex.Message), ex));
                }
            }

            GetData(sender, e);
            exceptions.Throw();
        }
        catch (Exception ex)
        {
            ReportException(ex);
        }
    }
Exemple #5
0
        public void ResizeToThumbnailTest()
        {
            string filename = Path.GetTempFileName();
            Console.WriteLine("Creating: {0}", filename);
            try
            {
                MemoryStream ms = new MemoryStream();
                AnimatedGifEncoder encoder = new AnimatedGifEncoder();
                encoder.SetFrameRate(5);
                encoder.Start(ms);
                for (char i = 'a'; i <= 'e'; i++)
                {
                    Console.Write(i.ToString());
                    encoder.AddFrame(ThumbnailBitmap.GetBitmapFromText(
                        i.ToString(), 48, 300, 200));
                }
                Console.WriteLine();
                encoder.Finish();
                ms.Flush();
                ms.Seek(0, SeekOrigin.Begin);

                ThumbnailBitmap bitmap = new ThumbnailBitmap(ms);
                FileStream fs = File.Create(filename);
                byte[] th = bitmap.Thumbnail;
                fs.Write(th, 0, th.Length);
                fs.Close();
            }
            finally
            {
                if (File.Exists(filename))
                    File.Delete(filename);
            }
        }
Exemple #6
0
    protected void files_FilesPosted(object sender, FilesPostedEventArgs e)
    {
        try
        {
            if (e.PostedFiles.Count == 0)
                return;

            ExceptionCollection exceptions = new ExceptionCollection();
            foreach (HttpPostedFile file in e.PostedFiles)
            {
                try
                {
                    TransitAccountPicture p = new TransitAccountPicture();

                    ThumbnailBitmap t = new ThumbnailBitmap(file.InputStream);
                    p.Bitmap = t.Bitmap;
                    p.Name = Path.GetFileName(file.FileName);
                    p.Description = string.Empty;
                    p.Hidden = true;

                    int id = SessionManager.CreateOrUpdate<TransitAccountPicture>(
                        p, SessionManager.AccountService.CreateOrUpdateAccountPicture);

                    Size size = t.GetNewSize(new Size(200, 200));

                    inputBody.Content = string.Format("<a href=AccountPictureView.aspx?id={2}><img border=0 width={0} height={1} src=AccountPicture.aspx?id={2}></a>\n{3}",
                        size.Width, size.Height, id, inputBody.Content);
                }
                catch (Exception ex)
                {
                    exceptions.Add(new Exception(string.Format("Error processing {0}: {1}",
                        Renderer.Render(file.FileName), ex.Message), ex));
                }
            }
            exceptions.Throw();
        }
        catch (Exception ex)
        {
            ReportException(ex);
        }
    }
Exemple #7
0
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            Nullable<Size> resize = Size;
            Nullable<DateTime> ims = IfModifiedSince;

            if (ims.HasValue)
            {
                if (ims.Value.ToUniversalTime().AddSeconds(CacheDuration) > DateTime.UtcNow)
                {
                    Response.StatusCode = 304;
                    return;
                }
            }

            TransitPicture p = null;

            switch (PageType)
            {
                case PicturePageType.Thumbnail:
                    
                    if (RequestId == 0)
                    {
                        p = GetRandomPictureWithThumbnail();

                        if (p == null)
                        {
                            p = new TransitPicture();
                            p.Id = 0;
                            p.Created = p.Modified = DateTime.Now;
                            p.Name = Guid.NewGuid().ToString();
                            if (!resize.HasValue) resize = new Size(100, 150);
                            p.Bitmap = ThumbnailBitmap.GetBitmapDataFromText("?", 72, 
                                resize.Value.Width, resize.Value.Height);
                        }
                    }
                    else
                    {
                        p = ims.HasValue ?
                                GetPictureWithThumbnail(RequestId, ims.Value) :
                                GetPictureWithThumbnail(RequestId);
                    }

                    break;
                case PicturePageType.Bitmap:
                    
                    p = ims.HasValue ?
                            GetPictureWithBitmap(RequestId, ims.Value) :
                            GetPictureWithBitmap(RequestId);

                    break;
            }

            if (p == null)
            {
                if (ims.HasValue)
                {
                    Response.StatusCode = 304;
                    return;
                }
                else
                {
                    throw new Exception("Invalid picture id.");
                }
            }

            Response.Cache.SetLastModified(p.Modified.ToLocalTime());
            Response.Cache.SetCacheability(HttpCacheability.Private);

            p.Name = (string.IsNullOrEmpty(p.Name)) ? p.Id.ToString() + ".jpg" : p.Id.ToString() + "-" + p.Name;

            if (resize.HasValue)
            {
                ThumbnailBitmap resized = new ThumbnailBitmap(new MemoryStream(p.Bitmap), new Size(0, 0),
                    resize.Value, resize.Value);

                switch (PageType)
                {
                    case PicturePageType.Bitmap:
                        p.Bitmap = resized.Bitmap;
                        break;
                    case PicturePageType.Thumbnail:
                        p.Bitmap = resized.Thumbnail;
                        break;
                }
            }

            switch (PageType)
            {
                case PicturePageType.Thumbnail:
                    p.Name.Insert(0, "thumbnail-");
                    break;
            }

            Response.ContentType = "image/" + Path.GetExtension(p.Name).TrimStart(".".ToCharArray());
            Response.AddHeader("Content-disposition", "attachment; filename=" + p.Name);
            Response.AddHeader("Created", p.Created.ToString("r"));
            Response.AddHeader("Modified", p.Modified.ToString("r"));
            Response.BinaryWrite(p.Bitmap);
            Response.End();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }
    protected void files_FilesPosted(object sender, FilesPostedEventArgs e)
    {
        try
        {
            if (e.PostedFiles.Count == 0)
                return;

            ExceptionCollection exceptions = new ExceptionCollection();
            foreach (HttpPostedFile file in e.PostedFiles)
            {
                try
                {
                    TransitAccountGroupPicture p = new TransitAccountGroupPicture();
                    ThumbnailBitmap t = new ThumbnailBitmap(file.InputStream);
                    p.Bitmap = t.Bitmap;
                    p.Name = Path.GetFileName(file.FileName);
                    p.Description = string.Empty;
                    p.AccountGroupId = RequestId;
                    SessionManager.CreateOrUpdate<TransitAccountGroupPicture>(
                        p, SessionManager.GroupService.CreateOrUpdateAccountGroupPicture);
                }
                catch (Exception ex)
                {
                    exceptions.Add(new Exception(string.Format("Error processing {0}: {1}",
                        Renderer.Render(file.FileName), ex.Message), ex));
                }
            }

            gridManage.CurrentPageIndex = 0;
            gridManage_OnGetDataSource(sender, e);
            gridManage.DataBind();
            exceptions.Throw();

            Redirect(string.Format("AccountGroupView.aspx?id={0}", RequestId));
        }
        catch (Exception ex)
        {
            ReportException(ex);
        }
    }
Exemple #9
0
        public int CreateAccountWithFacebook(string betapassword, string signature, string[] names, string[] values)
        {
            TransitFacebookLogin t_facebook = TryLoginFacebook(signature, names, values);
            using (SnCore.Data.Hibernate.Session.OpenConnection())
            {
                ISession session = SnCore.Data.Hibernate.Session.Current;

                string s = ManagedConfiguration.GetValue(session, "SnCore.Beta.Password", string.Empty);
                if (s != betapassword)
                {
                    throw new ManagedAccount.AccessDeniedException();
                }

                ManagedAccount acct = new ManagedAccount(session);

                // fetch account information
                Facebook.Session.ConnectSession facebookSession = new Facebook.Session.ConnectSession(
                    ManagedConfiguration.GetValue(session, "Facebook.APIKey", ""),
                    ManagedConfiguration.GetValue(session, "Facebook.Secret", ""));

                TransitAccount ta = new TransitAccount();
                NameValueCollectionSerializer facebookCookies = new NameValueCollectionSerializer(names, values);
                facebookSession.SessionKey = facebookCookies.Collection["session_key"];
                facebookSession.UserId = t_facebook.FacebookAccountId;
                Facebook.Rest.Api facebookAPI = new Facebook.Rest.Api(facebookSession);
                Facebook.Schema.user user = facebookAPI.Users.GetInfo();
                ta.Name = user.name;
                DateTime birthday = DateTime.Today;
                DateTime.TryParse(user.birthday_date, out birthday);
                ta.Birthday = birthday;
                acct.CreateWithFacebook(t_facebook.FacebookAccountId, user.proxied_email, 
                    ta, ManagedAccount.GetAdminSecurityContext(session));

                if (user.current_location != null)
                {
                    ta.City = user.current_location.city;

                    int country_id;
                    if (ManagedCountry.TryGetCountryId(session, user.current_location.country, out country_id))
                        ta.Country = user.current_location.country;
                    
                    int state_id;
                    if (ManagedState.TryGetStateId(session, user.current_location.state, user.current_location.country, out state_id))
                        ta.State = user.current_location.state;
                }

                if (user.picture_big != null)
                {
                    TransitAccountPicture t_picture = new TransitAccountPicture();
                    t_picture.AccountId = acct.Id;
                    ThumbnailBitmap bitmap = new ThumbnailBitmap(new Bitmap(user.picture_big));
                    t_picture.Bitmap = bitmap.Bitmap;
                    t_picture.Thumbnail = bitmap.Thumbnail;
                    t_picture.Name = user.pic;
                    ManagedAccountPicture m_picture = new ManagedAccountPicture(session);
                    m_picture.CreateOrUpdate(t_picture, ManagedAccount.GetAdminSecurityContext(session));
                }

                SnCore.Data.Hibernate.Session.Flush();
                return acct.Id;
            }
        }
Exemple #10
0
 public void TestResizeTransparentGIF89()
 {
     Stream sm = Assembly.GetExecutingAssembly().GetManifestResourceStream("SnCore.Tools.Tests.Images.transparent.gif");
     // load original image in decoder
     GifDecoder originaldecoder = new GifDecoder();
     originaldecoder.Read(sm);
     sm.Seek(0, SeekOrigin.Begin);
     GifDecoder.GifFrame frame = originaldecoder.GetFrame(0);
     Console.WriteLine("Transparency: {0} - {1}", frame.transparency, Color.FromArgb(frame.bgcolor));
     // load in ThumnailBitmap
     Assert.IsNotNull(sm);
     ThumbnailBitmap b = new ThumbnailBitmap(sm);
     Console.WriteLine("Size: {0}x{1}", b.FullSize.Width, b.FullSize.Height);
     // Assert.AreEqual(b.FullSize, new Size(227, 227));
     Assert.IsNotNull(b.Thumbnail);
     Assert.IsNotNull(b.Bitmap);
     // reload in decoder, verify transparency
     GifDecoder decoder = new GifDecoder();
     decoder.Read(new MemoryStream(b.Bitmap));
     frame = decoder.GetFrame(0);
     Console.WriteLine("Transparency: {0} - {1}", frame.transparency, Color.FromArgb(frame.bgcolor));
     //FileStream f = File.Create("C:\\temp\\chefout.gif");
     //f.Write(b.Bitmap, 0, b.Bitmap.Length);
     //f.Close();
     //f = File.Create("C:\\temp\\chefthumbnail.gif");
     //f.Write(b.Thumbnail, 0, b.Thumbnail.Length);
     //f.Close();
 }
Exemple #11
0
 public void TestTooSmallGIF89()
 {
     Stream sm = Assembly.GetExecutingAssembly().GetManifestResourceStream("SnCore.Tools.Tests.Images.small.gif");
     // load original image in decoder
     GifDecoder originaldecoder = new GifDecoder();
     originaldecoder.Read(sm);
     sm.Seek(0, SeekOrigin.Begin);
     GifDecoder.GifFrame frame = originaldecoder.GetFrame(0);
     Console.WriteLine("Transparency: {0} - {1}", frame.transparency, Color.FromArgb(frame.bgcolor));
     // load in ThumnailBitmap
     Assert.IsNotNull(sm);
     ThumbnailBitmap b = new ThumbnailBitmap(sm);
     Console.WriteLine("Size: {0}x{1}", b.FullSize.Width, b.FullSize.Height);
 }
Exemple #12
0
 public void TestLoadInvalid()
 {
     MemoryStream s = new MemoryStream();
     ThumbnailBitmap b = new ThumbnailBitmap(s);
     Console.WriteLine("Size: {0}x{1}", b.FullSize.Width, b.FullSize.Height);
 }