Пример #1
0
    public bool d = true;//debug mode

    public byte[] imageToByteArray(System.Drawing.Image imageIn)
    {
        int        t1 = Environment.TickCount;
        var        o  = System.Drawing.GraphicsUnit.Pixel;
        RectangleF r1 = imageIn.GetBounds(ref o);
        Rectangle  r2 = new Rectangle((int)r1.X, (int)r1.Y, (int)r1.Width, (int)r1.Height);

        System.Drawing.Imaging.BitmapData omg = ((Bitmap)imageIn).LockBits(r2, System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
        byte[] rgbValues = new byte[r2.Width * r2.Height * 4];
        Marshal.Copy((IntPtr)omg.Scan0, rgbValues, 0, rgbValues.Length);
        ((Bitmap)imageIn).UnlockBits(omg);
        Debug.Log("i2ba time: " + (Environment.TickCount - t1));
        //flip
        byte[] nbytes    = new byte[rgbValues.Length];//new byte
        var    RGBwidth  = r2.Width * 4;
        var    RGBheight = r2.Height;

        for (var y = 0; y < RGBheight; y++)
        {
            for (var x = 0; x < RGBwidth; x++)
            {
                nbytes[x + RGBwidth * y] = rgbValues[x + RGBwidth * (RGBheight - 1 - y)];
            }
        }
        return(nbytes);
    }
Пример #2
0
    public void LoadBackground(string path, int id)
    {
        try {
            System.Drawing.Image myImg = System.Drawing.Image.FromFile(path);
            var w    = myImg.Size.Width;
            var h    = myImg.Size.Height;
            var size = new Vector2(w, h);
            Debug.Log("format: " + myImg.PixelFormat);
            Texture2D tex = new Texture2D(w, h, TextureFormat.BGRA32, false);
            tex.LoadRawTextureData(imageToByteArray(myImg));
            tex.Apply();

            var    rr = new Rect(new Vector2(0, 0), size);
            Sprite sp = Sprite.Create(tex, rr, size);
            AddBackground(sp, Path.GetFileName(path), id);
        }
        catch (FileNotFoundException e)
        {
            Debug.Log(e);
            AddBackground(DefaultBackgroundSprite, "default", id);
        }
    }
Пример #3
0
 public void UpdateColumns()
 {
     ClearAllColumns();
     for (int i = 0; i < data.GetLength(); i++)
     {
         var bp         = data.BackgroundPathList[i];
         var mp         = data.MusicPathList[i];
         var n          = data.NameList[i];
         var o          = CreateColumnObject(i, bp, mp, n);
         var ImageChild = o.transform.GetChild(0);
         var r          = ImageChild.GetComponent <RectTransform>().rect;
         o.GetComponent <Column>().id = i;
         try
         {
             ColumnObjectList.Add(o);
             System.Drawing.Image myImg = System.Drawing.Image.FromFile(bp);
             var w    = myImg.Size.Width;
             var h    = myImg.Size.Height;
             var size = new Vector2(w, h);
             Debug.Log("format: " + myImg.PixelFormat);
             Texture2D tex = new Texture2D(w, h, TextureFormat.BGRA32, false);
             tex.LoadRawTextureData(imageToByteArray(myImg));
             tex.Apply();
             var im = ImageChild.gameObject.GetComponent <Image>();
             var rr = new Rect(new Vector2(0, 0), size);
             Debug.Log("r.size: " + r.size);
             r.size = size;
             Debug.Log(r);
             im.sprite = Sprite.Create(tex, rr, size);
         }catch (FileNotFoundException e)
         {
             Debug.Log(e);
         }
     }
     rt.sizeDelta = new Vector2(rt.sizeDelta.x, (data.GetLength() + 1) * VerticalGap / 2);
 }
Пример #4
0
        protected override void Awake()
        {
            if (string.IsNullOrEmpty(gifPath))
            {
                return;
            }

            _image = GetComponent <Image>();

            System.Drawing.Image gifImage  = System.Drawing.Image.FromFile(gifPath);
            FrameDimension       dimension = new FrameDimension(gifImage.FrameDimensionsList[0]);
            int frameCount = gifImage.GetFrameCount(dimension);

            for (int i = 0; i < frameCount; i++)
            {
                gifImage.SelectActiveFrame(dimension, i);

                Bitmap frame = new Bitmap(gifImage.Width, gifImage.Height);
                System.Drawing.Graphics.FromImage(frame).DrawImage(gifImage, Point.Empty);

                Texture2D frameTexture = new Texture2D(frame.Width, frame.Height);
                for (int x = 0; x < frame.Width; x++)
                {
                    for (int y = 0; y < frame.Height; y++)
                    {
                        System.Drawing.Color sourceColor = frame.GetPixel(x, y);
                        frameTexture.SetPixel(x, frame.Height - 1 - y, new Color32(sourceColor.R, sourceColor.G, sourceColor.B, sourceColor.A));
                    }
                }
                frameTexture.Apply();

                _gifFrameList.Add(Sprite.Create(frameTexture, new Rect(0, 0, frameTexture.width, frameTexture.height), Vector2.zero));
            }

            _isPlay = autoPlay;
        }