逐行创建位图文件

发布于 2024-08-16 17:09:29 字数 267 浏览 3 评论 0原文

是否可以用 C# 逐行编写位图?我担心在处理大文件时使用 Bitmap.Save() 函数会需要太多内存。

为了澄清,我想这样做:

  1. 打开文件并写入位图标头(如果有)
  2. 放入 1 个扫描线将图像数据写入缓冲区 (byte[])
  3. 将缓冲区的内容写入文件
  4. 除非不再有扫描线,否则从 2 开始重复
  5. 关闭文件

这在 C# 中是如何完成的?

谢谢,

克雷布

Is it possible to write a bitmap in C# line by line? I am worried that using the Bitmap.Save() function will require too much memory when dealing with LARGE files..

To clarify, I want to do this:

  1. Open the file and write the bitmap header (if any)
  2. Put 1 scanline worth of image data into a buffer (byte[])
  3. Write the contents of the buffer into the file
  4. Repeat from 2 unless there are no more scan lines
  5. Close the file

How is this done in C#?

Thanks,

kreb

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温柔一刀 2024-08-23 17:09:29

@Mikael

它看起来像这样:

int bufferSize = ((width +31) >> 5) << 2); // the width is in pixels, 1bpp,
// so I had to round up to the nearest 32 bits

byte[] buffer = new byte[bufferSize];
byte[] buffer = new byte[height * bufferSize];

for (int i = 0; i < height; i++)
{
    getLine(buffer,bufferSize);
    Buffer.BlockCopy(bufffer,0,bigBuffer, i*bufferSize, bufferSize);
}

unsafe
{
    fixed (byte* ptr = bigBuffer)
    {
        using (Bitmap image = new Bitmap(width,height,bufferSize,System.Drawing.Imaging.PixelFormat.Format1bbIndexed, new IntPtr(ptr)))
        {
            image.SetResolution(xres,yres);
            image.Save(filename,System.Drawing.Imaging.ImageFormat.Tiff);
        }
    }
}

我担心对于非常大的图像,bigBuffer可能会变得太大..我想将其更改为逐行写入文件,而不是一次性写入..

谢谢..: )

@Mikael

It looks like this:

int bufferSize = ((width +31) >> 5) << 2); // the width is in pixels, 1bpp,
// so I had to round up to the nearest 32 bits

byte[] buffer = new byte[bufferSize];
byte[] buffer = new byte[height * bufferSize];

for (int i = 0; i < height; i++)
{
    getLine(buffer,bufferSize);
    Buffer.BlockCopy(bufffer,0,bigBuffer, i*bufferSize, bufferSize);
}

unsafe
{
    fixed (byte* ptr = bigBuffer)
    {
        using (Bitmap image = new Bitmap(width,height,bufferSize,System.Drawing.Imaging.PixelFormat.Format1bbIndexed, new IntPtr(ptr)))
        {
            image.SetResolution(xres,yres);
            image.Save(filename,System.Drawing.Imaging.ImageFormat.Tiff);
        }
    }
}

I am worried that for really big images, bigBuffer might become too big.. I would like to change this to write to file line by line, instead of writing it in one go..

Thanks.. :)

微暖i 2024-08-23 17:09:29

我曾经编写过一些保存 32kx32k 像素 JPEG 文件的代码。我在创建、加载、查看和操作它时遇到了很多问题(Photoshop 可以处理最大 30kx30k 的文件)。
在尝试分配之后,我放弃了整个交易,并决定将大图像的一部分保存为小图像,并编写一些管理器程序,该程序知道如何在需要时“缝合”这些文件。

我不确定您什么时候进入,但如果它是为了显示,那么您可以查看 DeepZoom

I was once writing some code that saved a 32kx32k pixels JPEG file. I had tons of problems with creating it, loading it, viewing it and manipulating it (Photoshop can handle files up to 30kx30k).
After trying allot I dropped the whole deal and decided on saving parts of the big image to small images and write some manager program that knows how to "stitch" these files when needed.

I'm not sure when you are in to but if it is for display then you might check out DeepZoom

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文