由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)
{
}
}
}
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)
{
}
}
}
相关文章推荐
- 使用Python将文本转为图片
- 用三段 140 字符以内的代码生成一张 1024×1024 的图片
- PPT2007将Excel图表转为图片
- png图片 转为ico图标格式
- C# 图片转为二进制
- C#中查询数据库的图片system.byte[]怎么转为img
- Android图片转为16进制
- 【转】android:网络图片转为bitmap 保存至SD卡中
- OpenOffice 将word文档转为pdf再转为图片
- C#实现 word、pdf、ppt 转为图片
- 方形图片转为圆形图片
- 逆向工程——利用PowerDesigner将表结构转为物理数据模型并转换为图片
- 20161114---手写图片转为lmdb
- 网络图片url转为bitmap
- 将URL图片转为输出流返回前端
- 15行Python代码将图片转为Excel
- 在线将html转为图片的小程序(需要maven环境)
- C# 图片处理之:彩色图片转为黑白图
- iOS中如何将图片文件转为OpenCV中的cv::Mat
- c#byte(二进制)转为Image(图片)