您的位置:首页 > 其它

由3072*2048的图片转为1024*1024的图片

2016-12-10 16:08 162 查看
直接上代码

 public partial class Form1 : Form

    {

        [DllImport("jpgDeocder.dll", EntryPoint = "tjpeg2rgb", CallingConvention = CallingConvention.Cdecl)]

        public static extern int tjpeg2rgb(Byte[] ipegData, Int32 jpegSize, Byte[] rgbData, out Int32 rgbSize);

        [DllImport("jpgDeocder.dll", EntryPoint = "trgb2jpeg", CallingConvention = CallingConvention.Cdecl)]

        public static extern int trgb2jpeg(Byte[] rgbData, Int32 width, Int32 height, Int32 quality, Byte[][] jpeg_buffer, out Int32 jpegSize);

        [DllImport("jpgDeocder.dll", EntryPoint = "savejpg", CallingConvention = CallingConvention.Cdecl)]

        public static extern int savejpg(Byte[] rgb_buffer, Int32 width, Int32 height, string fileNmae, Int32 quality);

        [DllImport("JpegToRGB.dll", EntryPoint = "savebmp", CallingConvention = CallingConvention.Cdecl)]

        public static extern int savebmp(byte[] pdata, String bmp_file, int width, int height, int flag);

        Byte[] jpgBuf=new Byte[3072*2048*3];

        Byte[] rgbBuf = new Byte[3072 * 2048 * 3];

         Byte[] dis= new Byte[1024*1024* 3];//

        Byte[] data= new Byte[2048 * 2048 * 3];//中间变量

        int iJpegWidth = 0;

        int k = 0;

        public Form1()

        {

            InitializeComponent();

            jpgBuf = FileContent("1.jpg");

            tjpeg2rgb(jpgBuf, jpgBuf.Length, rgbBuf, out iJpegWidth);//转为rgb

          //  savejpg(rgbBuf, 2048, 2048, "D:\\TryOnTest_save\\JpgTryOnToBmp" + "2.jpg", 1);

            CutImage(rgbBuf, data);

          //savejpg(data,2048,2048, "cut.jpg", 100);

           /*

            for (int j = 0; j < 2048; j+=2)

            {

               

                Array.ConstrainedCopy(data, j * 2048 * 3, src, (j*2048*3)/2, 2048 * 3);

                k=j;

               // scale(src,dis,2048);

            }

            */

            Scale(data, dis);

           // savejpg(dis, 1024, 1024, "D:\\TryOnTest_save\\JpgTryOnToBmp" + "1.jpg", 100);

            savebmp(dis, "scale.bmp", 1024, 1024, 1);

        }

        private void Scale(Byte []srcPic, byte []dstPic )

        {

            Byte[] tempBuf1 = new Byte[2048 * 3];

            Byte[] tempBuf2 = new Byte[1024 * 3];

            for (int i = 0; i < 1024; i ++)

            {

                Array.Copy(srcPic, i * 2048 * 3 *  2,  tempBuf1, 0, 2048 * 3);

                OnelineScan(tempBuf1, tempBuf2, 2048);

                Array.Copy(tempBuf2, 0, dstPic, i  * 1024 * 3, 1024 * 3);

            }

        }

        private void OnelineScan(Byte[] src,Byte[] dis,int width)

        {    

            for (int i = 0;  i < 1024 ;  i++)

            { 

                Array.ConstrainedCopy(src, i * 3* 2 , dis, i*3, 3);   //

            }

        }

        private byte[] FileContent(string fileName)

        {

            FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);//需要引用using system Io;

            try

            {

                byte[] jpgBuff = new byte[fs.Length];

                fs.Read(jpgBuff, 0, (int)fs.Length);

                return jpgBuff;

            }

            catch (Exception ex)

            {

                return null;

            }

            finally

            {

                if (fs != null)

                {

                    //关闭资源  

                    fs.Close();

                }

            }

        }

        public  void CutImage(Byte[] srcBuf, Byte[] dstBuf)

        {

            //Byte[] tmpBuf = new byte[2048 * 3];

            int i;

            for (i = 0; i < 2048; i++)

            {

                //    Array.ConstrainedCopy(srcBuf, i * 3072 * 3 + 512, tmpBuf, 0, 2048 * 3);

                //tmpBuf.Reverse();

                //  Array.ConstrainedCopy(tmpBuf,0, dstBuf, i * 2048 * 3, 2048 * 3);

                Array.ConstrainedCopy(srcBuf, i * 3072 * 3 + 512 * 3, dstBuf, i * 2048 * 3, 2048 * 3);

            }

        }

        private void Form1_Load(object sender, EventArgs e)

        {

        }

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: