【发布时间】:2021-09-05 00:42:13
【问题描述】:
位图图像乘法 我找到了这个内存流选项来合并具有给定重复次数的文件。 所有的东西都是工作文件,但最后,生成的 BMP 文件不是按输入重复的。 需要帮忙。 代码中的注释行是我尝试过的另一种方式。 在此处输入代码 公共部分类Form1:表格 { FileStream 位图文件加载; 位图 bmp; int NoOfRepeats; 公共表格1() { 初始化组件(); }
private void btnOpenFile_Click(object sender, EventArgs e)
{
DoubleBuffered = true;
OpenFileDialog openFileDialog1 = new OpenFileDialog
{
InitialDirectory = @"D:\",
Title = "Browse Design Files",
CheckFileExists = true,
CheckPathExists = true,
DefaultExt = "tif",
Filter = "Images (*.BMP;*.TIF)|*.BMP;*.TIF|" + "All files (*.*)|*.*",
FilterIndex = 1,
RestoreDirectory = true,
Multiselect = false,
ReadOnlyChecked = false,
ShowReadOnly = true
};
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
byte[] ba = null;
foreach (String file in openFileDialog1.FileNames)
{
Image temp = new Bitmap(file);
LblSourceWidth.Text = Convert.ToString(temp.Width);
LblSourHeight.Text = Convert.ToString(temp.Height);
temp.Dispose();
bitmapfileload = new FileStream(file,FileMode.Open,FileAccess.Read,FileShare.ReadWrite);
ba = streamToByteArray(bitmapfileload);
}
NoOfRepeats = int.Parse(ltbRepeat.Text); // Text box will give no of repeat for writing proceedure
// List <byte[]> ListBa = new List<byte[]>();
MemoryStream MergeFile = new MemoryStream();
using (MemoryStream allFrameStream = new MemoryStream())
{
for (int i = 0; i < NoOfRepeats; i++)
{
allFrameStream.Write(ba, 0, ba.Length);
allFrameStream.Position = ba.Length * i;
//ListBa.Add(allFrameStream.ToArray());
//ListBa.Add(ba);
}
MergeFile = allFrameStream;
bmp = new Bitmap(MergeFile);
}
//byte[] finaleba = Combine(ListBa.ToArray());
//byte[] finaleba = ListBa.Cast<byte[]>().SelectMany(a => a).ToArray();
//LblListCount.Text = Convert.ToString(finaleba.Length); // List Count = no of repeats for FOR-LOOP
//Stream Finalfile = new MemoryStream(MergeFile);
//richTextBox1.Text = Convert.ToString(Finalfile);
LblDestiWidth.Text = Convert.ToString(bmp.Width); // no change here
LblDestiHeight.Text = Convert.ToString(bmp.Height); // no change here
pictureBox1.Image = bmp;
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
}
}
private static byte[] Combine(byte[][] arrays)
{
byte[] bytes = new byte[arrays.Sum(a => a.Length)];
int offset = 0;
foreach (byte[] array in arrays)
{
Buffer.BlockCopy(array, 0, bytes, offset, array.Length);
offset += array.Length;
}
return bytes;
}
public static byte[] streamToByteArray(Stream input)
{
MemoryStream ms = new MemoryStream();
input.CopyTo(ms);
return ms.ToArray();
}
【问题讨论】:
-
图片大小不同。因此,当您将图像添加到内存流时,您需要在前面加上字节数。然后在删除每个图像时读取字节数,然后根据字节大小获取图像。
-
感谢@jdweng 的及时回复。我使用重复次数不重复的 For 循环多次重复相同的图像。我还检查了数组长度,发现数组长度根据重复次数增加,但显示的图像大小与原始大小相同。
-
这很有意义。如果将多个图像添加到数组中,则每个图像的大小不会改变。但是整个数组的大小变大了。
-
如何将所有图像按长度写入流???这实际上是我正在做的。因此,如果流长度增加,那么图像长度也应该增加,但它没有。你能建议对代码进行任何更正吗???
-
请在此处解释您想要的实际最终目标,您似乎希望简单地将所有字节从保存这些文件到一个大字节数组中以某种方式导致 1 个具有所有这些像素的图像?这不是图像文件格式的工作原理。
标签: c#