Ejemplo n.º 1
0
        internal static Image MakeBmp(string path, bool autosave = false, bool crop = true)
        {
            Clim bclim = Bclim.Analyze(path);

            if (bclim.Magic != 0x4D494C43)
            {
                return(null);
            }

            // Interpret data.
            int f = bclim.FileFormat;

            if (f > 13)
            {
                return(null);
            }

            Bitmap img;

            if (f == 7 && BitConverter.ToUInt16(bclim.Data, 0) == 2)
            {
                // PKM XY Format 7 (Color Palette)
                img = Bclim.GetIMG_XY7(bclim);
            }
            else if (f == 10 || f == 11)
            {
                img = Bclim.GetIMG_ETC(bclim);
            }
            else
            {
                img = Bclim.GetImg(bclim);
            }

            if (img == null)
            {
                return(null);
            }
            Rectangle cropRect = new Rectangle(0, 0, bclim.Width, bclim.Height);
            Bitmap    cropBmp  = new Bitmap(cropRect.Width, cropRect.Height);

            using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(cropBmp))
            {
                g.DrawImage(img,
                            new Rectangle(0, 0, cropBmp.Width, cropBmp.Height),
                            cropRect,
                            GraphicsUnit.Pixel);
            }
            if (!autosave)
            {
                return(!crop
                                                   ? img
                                                   : cropBmp);
            }

            using (MemoryStream ms = new MemoryStream())
            {
                //error will throw from here
                cropBmp.Save(ms, ImageFormat.Png);
                byte[] data = ms.ToArray();
                File.WriteAllBytes(bclim.FilePath + "\\" + bclim.FileName + ".png", data);
            }
            return(!crop
                                           ? img
                                           : cropBmp);
        }