现在又N个不同格式的图像,要求如下:
合并所有的图像。
1、所有的图像的头指针(*header)都去掉
2、然后合并成一个文件(连接方法不限)
3、自己可以设置输出的图像文件的长度、高度、颜色、旋转角度等4个属性。
4、所有的图像可以不同格式(bmp,jpg,gif,tif,png),但是输出必须是BMP格式,而且合并成一个文件。
5、选择最少一个图像文件
下面有我做好的框架,我就是做不出来。我做出的是N个FileStream合并。但是显示的确是第一个图片真郁闷啊。
由于论坛不给上传图片,我晚一些补上来。
合并所有的图像。
1、所有的图像的头指针(*header)都去掉
2、然后合并成一个文件(连接方法不限)
3、自己可以设置输出的图像文件的长度、高度、颜色、旋转角度等4个属性。
4、所有的图像可以不同格式(bmp,jpg,gif,tif,png),但是输出必须是BMP格式,而且合并成一个文件。
5、选择最少一个图像文件
下面有我做好的框架,我就是做不出来。我做出的是N个FileStream合并。但是显示的确是第一个图片真郁闷啊。
由于论坛不给上传图片,我晚一些补上来。
FileStream addStream=new FileStream(objPath,FileMode.OpenOrCreate); //合并的流
BinaryWriter addWriter= new BinaryWriter(addStream); //合并用的BinaryWriter //合并以下的图片:
foreach (object obj in listFile.Items) //循环获取列表中的文件名字
{
string srcPath = (string)obj;
FileStream tempStream = new FileStream(srcPath, FileMode.Open); //打开列表中的文件
BinaryReader tempReader = new BinaryReader(tempStream);
addWriter.Write(tempReader.ReadBytes((int)tempStream.Length));
tempReader.Close();
tempStream.Close();
}
addWriter.Close();
addStream.Close(); Image im = Image.FromFile("C:\\output.bmp");
Bitmap bm = (Bitmap)im.GetThumbnailImage(800, 600, () => { return false; }, IntPtr.Zero);
bm.Save("c:\\output1.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
im.Dispose();
bm.Dispose();
应该是将图片的数据生成Image对象,然后都绘制到一个新的Image上,可以设置绘制的位置、可见性(Opacity)、大小等。
数据流操作不太现实,每种格式的流都不同。
简单的方案是:
1、新建一个图片,设置size为合并后的size。(可能需要计算)
2、将需要合并的图像绘制到新建的图片中,绘制的位置需要计算。
3、保存新建的图片到bmp文件中。
其次,阅读和学习 《GDI+使用帮助》
最后,就可以自己写出来了。
我的原理很简单,计算出宽度最大的图片,然后把所有图片合成一列。
可能有点累赘哦。发一些代码给你们看看吧。 /************************************/
//获得输出文件的总高度、最大宽度
//achieve the width and height of output file
int totalHeight = 0;
int totalWidth = 0;
foreach (object obj in listFile.Items)
{
Image i = Image.FromFile(obj.ToString());
totalHeight += i.Height;
if (totalWidth < i.Width)
{
totalWidth = i.Width;
}
i.Dispose();
}
//获取结束
//achieve ... end
/*********************************/ Bitmap outputBMP = new Bitmap(totalWidth, totalHeight);
int recordHeight = 0; //记录目标文件的复制高度
foreach (object obj in listFile.Items)
{
//读取文件 //read image
Bitmap tempBMP = (Bitmap)Image.FromFile(obj.ToString());
//复制像素 //copy pixel
for (int row = 0; row < tempBMP.Width; row++) //行,宽
{
for (int col = 0; col < tempBMP.Height; col++) //列,高
{
Color tempColor = tempBMP.GetPixel(row, col);
outputBMP.SetPixel(row, col + recordHeight, tempColor);
}
}
//源图片不够长的时候,用白色补足。
//when width of resource image is not enough, set the pixel "color white"
if (totalWidth > tempBMP.Width)
{
for (int row = tempBMP.Width; row < totalWidth; row++)
{
for (int col = 0; col < tempBMP.Height; col++)
{
outputBMP.SetPixel(row, col + recordHeight, Color.White);
}
}
}
//记录复制的高度
recordHeight += tempBMP.Height;
tempBMP.Dispose();
}