Example #1
0
    /// <summary>
    /// useLinear:是否使用Linear Color Space
    /// </summary>
    /// <param name="data"></param>
    /// <param name="useLinear"></param>
    /// <returns></returns>
    public static Texture2D Decode(byte[] data, bool useLinear)
    {
        if (mInstance == null)
        {
            mInstance = new GIFDecoder();
        }

        if (mInstance.Read(data, useLinear) == STATUS_OK)
        {
            Texture2D tex = mInstance.mTexture;
            mInstance.mTexture = null;
            return(tex);
        }

        return(null);
    }
Example #2
0
        private void LoadImage(FileUpload fu)
        {
            if (fu != null && fu.DisplayThumbnail)
            {
                try
                {
                    if ((fu.Name.ToLower().EndsWith("jpg") || fu.Name.ToLower().EndsWith("jpeg") || fu.Name.ToLower().EndsWith("png")))
                    {
                        BitmapImage imageSource = new BitmapImage();

                        using (var stream = fu.File.OpenRead())
                        {
                            imageSource.SetSource(stream);
                        }
                        m_previewImage.Source     = imageSource;
                        m_previewImage.Visibility = Visibility.Visible;
                    }
                    else if (fu.Name.ToLower().EndsWith("gif"))
                    {
                        GifImage gif = null;
                        using (var stream = fu.File.OpenRead())
                        {
                            gif = GIFDecoder.Decode(stream);
                            if (gif.Frames.Count > 0)
                            {
                                m_previewImage.Source     = gif.Frames[0].Image;
                                m_previewImage.Visibility = Visibility.Visible;
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    string message = e.Message;
                }
            }
            else
            {
                Dispatcher.BeginInvoke(() =>
                {
                    m_previewImage.Visibility = Visibility.Collapsed;
                });
            }
        }
Example #3
0
    void SetSprite(string path, Image image)
    {
        if (File.Exists(path))
        {
            byte[]    bytes = File.ReadAllBytes(path);
            Texture2D tex   = null;
            if (bytes[0] == 'G' && bytes[1] == 'I' && bytes[2] == 'F')
            {
                tex = GIFDecoder.Decode(bytes, false);
            }
            else
            {
                tex = new Texture2D(1, 1, TextureFormat.ARGB32, false);
                tex.LoadImage(bytes);
            }

            if (tex != null)
            {
                Sprite sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), Vector2.zero);
                image.sprite = sprite;
            }
        }
    }
Example #4
0
        void m_addFilesButton_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();

            dlg.Filter      = Filter;
            dlg.Multiselect = MultiSelect;
            if (!MultiUpload)
            {
                dlg.Multiselect = false;
            }

            if ((bool)dlg.ShowDialog())
            {
                if (!MultiUpload)
                {
                    m_files.Clear();
                }

                if (CheckUploadFilesEvent != null)
                {
                    CheckUploadFileEventArgs checkArgs = new CheckUploadFileEventArgs(new List <FileInfo>(dlg.Files));
                    CheckUploadFilesEvent(this, checkArgs);
                    if (checkArgs.CheckResult == false)
                    {
                        return;
                    }
                }

                BitmapSource imageSource = null;

                foreach (FileInfo file in dlg.Files)
                {
                    string fileName = file.Name.ToLower().Trim();
                    var    suffix   = file.Extension;
                    //如果文件没有后缀名,需要提示User
                    if (string.IsNullOrEmpty(suffix))
                    {
                        this.ShowMessage(MessageResource.Uploader_InvalidFileExtension, Newegg.Oversea.Silverlight.Controls.Components.MessageType.Warning);
                        continue;
                    }

                    if (!this.Filter.Contains("*.*") && !this.Filter.ToLower().Contains(suffix.ToLower()))
                    {
                        this.ShowMessage(MessageResource.Uploader_InvalidFileExtension, Newegg.Oversea.Silverlight.Controls.Components.MessageType.Warning);
                        continue;
                    }
                    try
                    {
                        using (file.OpenRead())
                        {
                            if (fileName.EndsWith("jpg") || fileName.EndsWith("jpeg") || fileName.EndsWith("png"))
                            {
                                imageSource = new BitmapImage();
                                using (var stream = file.OpenRead())
                                {
                                    try
                                    {
                                        imageSource.SetSource(stream);
                                    }
                                    catch
                                    {
                                        //捕获特定异常,比如把a.xls改成a.jpg,此时需要给一个友好提示
                                        this.ShowMessage(MessageResource.Uploader_InvalidImageType, Newegg.Oversea.Silverlight.Controls.Components.MessageType.Warning);
                                        continue;
                                    }
                                }
                            }
                            else if (fileName.EndsWith("gif"))
                            {
                                GifImage gif = null;
                                using (var stream = file.OpenRead())
                                {
                                    try
                                    {
                                        gif = GIFDecoder.Decode(stream);
                                    }
                                    catch
                                    {
                                        //捕获特定异常,比如把a.jpg改成a.gif,此时需要给一个友好提示
                                        this.ShowMessage(MessageResource.Uploader_InvalidImageType, Newegg.Oversea.Silverlight.Controls.Components.MessageType.Warning);
                                        continue;
                                    }
                                    if (gif.Frames.Count > 0)
                                    {
                                        imageSource = gif.Frames[0].Image;
                                    }//如果user把jpg文件的后缀修改为.gif,需要抛异常提示User
                                    else
                                    {
                                        this.ShowMessage(MessageResource.Uploader_InvalidImageType, Newegg.Oversea.Silverlight.Controls.Components.MessageType.Warning);
                                        continue;
                                    }
                                }
                            }
                        }
                    }//检查文件是否正在使用
                    catch (IOException)
                    {
                        this.ShowMessage(MessageResource.Uploader_FileInUsed, Newegg.Oversea.Silverlight.Controls.Components.MessageType.Error);
                        continue;
                    }
                    //Catch other exception
                    catch (Exception ex)
                    {
                        this.ShowMessage(ex.Message, Newegg.Oversea.Silverlight.Controls.Components.MessageType.Error);
                        continue;
                    }

                    //check image width and height

                    if (this.DimensionsMatch != DimensionsMatchType.None)
                    {
                        if (fileName.EndsWith("jpg") || fileName.EndsWith("png") || fileName.EndsWith("gif") ||
                            fileName.EndsWith("jpeg") || fileName.EndsWith("bmp"))
                        {
                            try
                            {
                                if (imageSource != null)
                                {
                                    CheckImageDimensions(imageSource, file);
                                }
                            }
                            // Catch check exception
                            catch (ArgumentException ex)
                            {
                                this.ShowMessage(ex.Message, Newegg.Oversea.Silverlight.Controls.Components.MessageType.Warning);
                                continue;
                            }
                        }
                    }

                    FileUpload upload = new FileUpload(this.Dispatcher, UploadUrl, file, this);
                    upload.UploadCanceled += (o, b) =>
                    {
                        if (this.UploadCanceled != null)
                        {
                            UploadCanceled(this, b);
                        }
                    };

                    upload.CanEditFileName = CanEditFileName;

                    if (UploadChunkSize > 0)
                    {
                        upload.ChunkSize = UploadChunkSize;
                    }
                    if (MaximumFileNameLength > 0 && upload.File.Name.Length > MaximumFileNameLength)
                    {
                        this.ShowMessage(string.Format(MessageResource.Uploader_GreaterThanMaximumFileNameLength, upload.Name, upload.File.Name.Length, this.MaximumFileNameLength), Newegg.Oversea.Silverlight.Controls.Components.MessageType.Warning);
                        continue;
                    }

                    //单个文件超过限制的大小
                    if (MaximumUploadBytes >= 0 && upload.FileLength > MaximumUploadBytes)
                    {
                        this.ShowMessage(string.Format(MessageResource.Uploader_OutOfSingleAvailableUploadSize, upload.Name, this.MaximumUploadSize), Newegg.Oversea.Silverlight.Controls.Components.MessageType.Warning);
                        continue;
                    }

                    //全部的文件Size超过限制大小
                    if (MaximumTotalUploadBytes >= 0 && TotalUploadSize + upload.FileLength > MaximumTotalUploadBytes)
                    {
                        this.ShowMessage(string.Format(MessageResource.Uploader_OutOfTotalAvailableUploadSize, this.MaximumTotalUploadSize), Newegg.Oversea.Silverlight.Controls.Components.MessageType.Warning);
                        break;
                    }

                    //多文件上传的时候才作此Check
                    if (MaxNumberToUpload > -1 && MultiUpload)
                    {
                        if (++m_count > MaxNumberToUpload)
                        {
                            this.ShowMessage(string.Format(MessageResource.Uploader_OutOfMaximumNumber, this.MaxNumberToUpload), Newegg.Oversea.Silverlight.Controls.Components.MessageType.Warning);
                            break;
                        }
                    }
                    upload.DisplayThumbnail = (bool)this.m_displayThumbailCheckBox.IsChecked;
                    if (!this.AllowThumbnail)
                    {
                        upload.DisplayThumbnail = false;
                    }
                    upload.StatusChanged         += new EventHandler(upload_StatusChanged);
                    upload.UploadProgressChanged += new ProgressChangedEvent(upload_UploadProgressChanged);
                    upload.UploadCompleted       += new UploadCompletedEvent(upload_UploadCompleted);
                    m_files.Add(upload);
                    this.m_borderBackground.Visibility = System.Windows.Visibility.Collapsed;
                }
            }
        }
Example #5
0
        private void hbAddImage_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openDialog = new OpenFileDialog();

            if (openDialog.ShowDialog() == true)
            {
                using (Stream stream = openDialog.File.OpenRead())
                {
                    // Don't allow big files.
                    if (stream.Length > 5000000)
                    {
                        MessageBox.Show("图片尺寸必须小于5MB.");
                    }
                    else
                    {
                        ////BitmapImage image = new BitmapImage();
                        ////byte[] data = new byte[stream.Length];
                        ////stream.Read(data, 0, (int)stream.Length);

                        ////tmpImageName = openDialog.File.Name.ToString();
                        ////tmpImageStream = data;
                        ////image.SetSource(stream);
                        ////imgLoad.Source = image;
                        ////imgLoad.Visibility = Visibility.Visible;
                        ////stream.Close();

                        string      fileExtension = System.IO.Path.GetExtension(openDialog.File.Name.ToString());
                        BitmapImage image         = new BitmapImage();

                        byte[] data = new byte[stream.Length];
                        //stream.Read(data, 0, (int)stream.Length);

                        //tmpImageName = openDialog.File.Name.ToString();
                        //tmpImageStream = data;
                        ////image.SetSource(stream);
                        ////imgOwner.Source = image;
                        //stream.Close();

                        GifImage tmpgif = null;

                        if (fileExtension == ".gif")
                        {
                            //stream.Read(data, 0, (int)stream.Length);
                            //ExtendedImage extendedImage = new ExtendedImage();
                            //extendedImage.SetSource(stream);
                            //cusImage.Source = extendedImage;

                            tmpgif       = GIFDecoder.Decode(stream);
                            tmpImageName = openDialog.File.Name.ToString();

                            for (int count = 0; count < tmpgif.Frames.Count; count++)
                            {
                                imgLoad.Source     = tmpgif.Frames[count].Image;
                                imgLoad.Visibility = Visibility.Visible;
                                //imgOwner.Source = tmpgif.Frames[count].Image;
                                //image.SetSource(imgOwner.Source);
                            }

                            //byte[] data = new byte[stream.Length];
                            stream.Read(tmpgif.GlobalColorTable, 0, (int)tmpgif.GlobalColorTable.Length);
                            tmpImageStream = tmpgif.GlobalColorTable;
                            //image.SetSource(stream);
                        }
                        else
                        {
                            stream.Read(data, 0, (int)stream.Length);
                            tmpImageName   = openDialog.File.Name.ToString();
                            tmpImageStream = data;
                            image.SetSource(stream);
                            imgLoad.Source     = image;
                            imgLoad.Visibility = Visibility.Visible;
                        }

                        stream.Close();
                    }
                }
            }
        }