C#将1GB大图裁剪为8张图片

C#处理超大图片(1GB)需要特别注意内存管理和性能优化。以下是几种高效裁剪方案:

方法1:使用System.Drawing分块处理(内存优化版)

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;class Program
{static void Main(){string sourceImagePath = "1GB_Image.bmp";string outputFolder = "CroppedImages";if (!Directory.Exists(outputFolder)){Directory.CreateDirectory(outputFolder);}// 获取图片尺寸但不加载全部内容using (var image = Image.FromFile(sourceImagePath)){int totalWidth = image.Width;int totalHeight = image.Height;// 计算每块尺寸 (2x4网格)int chunkWidth = totalWidth / 2;int chunkHeight = totalHeight / 4;// 分块裁剪for (int row = 0; row < 4; row++){for (int col = 0; col < 2; col++){int x = col * chunkWidth;int y = row * chunkHeight;// 确保最后一块包含剩余部分int width = (col == 1) ? totalWidth - x : chunkWidth;int height = (row == 3) ? totalHeight - y : chunkHeight;CropImage(sourceImagePath,Path.Combine(outputFolder, $"part_{row}_{col}.jpg"),x, y, width, height);}}}}static void CropImage(string sourcePath, string destPath, int x, int y, int width, int height){// 使用流式处理避免全图加载using (var source = new Bitmap(sourcePath))using (var dest = new Bitmap(width, height))using (var graphics = Graphics.FromImage(dest)){graphics.DrawImage(source,new Rectangle(0, 0, width, height),new Rectangle(x, y, width, height),GraphicsUnit.Pixel);// 保存为JPEG减少体积dest.Save(destPath, ImageFormat.Jpeg);Console.WriteLine($"已保存: {destPath} ({width}x{height})");}}
}

方法2:使用ImageSharp(现代跨平台方案)

首先安装NuGet包:

Install-Package SixLabors.ImageSharp

实现代码:

using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Formats.Jpeg;class Program
{static async Task Main(){string sourcePath = "1GB_Image.jpg";string outputDir = "CroppedImages";Directory.CreateDirectory(outputDir);// 配置内存选项处理大图var configuration = Configuration.Default.Clone();configuration.MemoryAllocator = new SixLabors.ImageSharp.Memory.ArrayPoolMemoryAllocator();// 分块加载和处理using (var image = await Image.LoadAsync(configuration, sourcePath)){int totalWidth = image.Width;int totalHeight = image.Height;int chunkWidth = totalWidth / 2;int chunkHeight = totalHeight / 4;for (int row = 0; row < 4; row++){for (int col = 0; col < 2; col++){int x = col * chunkWidth;int y = row * chunkHeight;int width = (col == 1) ? totalWidth - x : chunkWidth;int height = (row == 3) ? totalHeight - y : chunkHeight;// 克隆并裁剪区域using (var cropped = image.Clone(ctx => ctx.Crop(new Rectangle(x, y, width, height)))){string outputPath = Path.Combine(outputDir, $"part_{row}_{col}.jpg");await cropped.SaveAsync(outputPath, new JpegEncoder {Quality = 80 // 适当压缩});Console.WriteLine($"已保存: {outputPath}");}}}}}
}

方法3:使用内存映射文件处理超大图

using System;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Drawing;
using System.Drawing.Imaging;class Program
{static void Main(){string sourcePath = "1GB_Image.bmp";string outputDir = "CroppedImages";Directory.CreateDirectory(outputDir);// 获取BMP文件头信息var bmpInfo = GetBmpInfo(sourcePath);int width = bmpInfo.Width;int height = bmpInfo.Height;int bytesPerPixel = bmpInfo.BitsPerPixel / 8;int stride = width * bytesPerPixel;// 计算分块int chunkWidth = width / 2;int chunkHeight = height / 4;// 使用内存映射文件处理using (var mmf = MemoryMappedFile.CreateFromFile(sourcePath, FileMode.Open)){for (int row = 0; row < 4; row++){for (int col = 0; col < 2; col++){int x = col * chunkWidth;int y = row * chunkHeight;int cropWidth = (col == 1) ? width - x : chunkWidth;int cropHeight = (row == 3) ? height - y : chunkHeight;// 创建目标位图using (var dest = new Bitmap(cropWidth, cropHeight, PixelFormat.Format24bppRgb)){var destData = dest.LockBits(new Rectangle(0, 0, cropWidth, cropHeight),ImageLockMode.WriteOnly,dest.PixelFormat);try{// 计算源文件偏移量(BMP文件头54字节 + 数据偏移)long offset = 54 + (height - y - 1) * stride + x * bytesPerPixel;// 逐行复制for (int line = 0; line < cropHeight; line++){using (var accessor = mmf.CreateViewAccessor(offset - line * stride, cropWidth * bytesPerPixel)){byte[] lineData = new byte[cropWidth * bytesPerPixel];accessor.ReadArray(0, lineData, 0, lineData.Length);IntPtr destPtr = destData.Scan0 + (line * destData.Stride);System.Runtime.InteropServices.Marshal.Copy(lineData, 0, destPtr, lineData.Length);}}}finally{dest.UnlockBits(destData);}string outputPath = Path.Combine(outputDir, $"part_{row}_{col}.jpg");dest.Save(outputPath, ImageFormat.Jpeg);Console.WriteLine($"已保存: {outputPath}");}}}}}static (int Width, int Height, int BitsPerPixel) GetBmpInfo(string filePath){using (var fs = new FileStream(filePath, FileMode.Open))using (var reader = new BinaryReader(fs)){// 读取BMP头if (reader.ReadChar() != 'B' || reader.ReadChar() != 'M')throw new Exception("不是有效的BMP文件");fs.Seek(18, SeekOrigin.Begin); // 跳转到宽度信息int width = reader.ReadInt32();int height = reader.ReadInt32();fs.Seek(28, SeekOrigin.Begin); // 跳转到位深信息int bitsPerPixel = reader.ReadInt16();return (width, height, bitsPerPixel);}}
}

方法4:使用Magick.NET(专业图像处理)

首先安装NuGet包:

Install-Package Magick.NET-Q16-x64

实现代码:

using ImageMagick;
using System;
using System.IO;class Program
{static void Main(){string sourcePath = "1GB_Image.tif";string outputDir = "CroppedImages";Directory.CreateDirectory(outputDir);// 设置资源限制MagickNET.SetResourceLimit(ResourceType.Memory, 1024 * 1024 * 1024); // 1GB// 使用像素流处理大图using (var image = new MagickImage(sourcePath)){int width = image.Width;int height = image.Height;int chunkWidth = width / 2;int chunkHeight = height / 4;for (int row = 0; row < 4; row++){for (int col = 0; col < 2; col++){int x = col * chunkWidth;int y = row * chunkHeight;int cropWidth = (col == 1) ? width - x : chunkWidth;int cropHeight = (row == 3) ? height - y : chunkHeight;using (var cropped = image.Clone(new MagickGeometry{X = x,Y = y,Width = cropWidth,Height = cropHeight})){string outputPath = Path.Combine(outputDir, $"part_{row}_{col}.jpg");cropped.Quality = 85;cropped.Write(outputPath);Console.WriteLine($"已保存: {outputPath}");}}}}}
}

裁剪方案选择建议

方法        优点缺点使用场景
System.Drawing内置库,简单内存占用高Windows小图处理
ImageSharp跨平台,现代API    学习曲线需要跨平台支持
内存映射内存效率高复杂,仅限BMP超大图处理
Magick.NET功能强大需要安装专业图像处理

注意事项

  1. 内存管理:处理1GB图片需要至少2-3GB可用内存
  2. 文件格式:BMP/TIFF适合处理,JPEG可能有压缩问题
  3. 磁盘空间:确保有足够空间存放输出文件
  4. 异常处理:添加try-catch处理IO和内存不足情况
  5. 性能优化:
  • 使用64位应用程序
  • 增加GC内存限制:<gcAllowVeryLargeObjects enabled="true"/>
  • 分批处理减少内存压力

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.pswp.cn/news/905902.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Linux系统启动相关:vmlinux、vmlinuz、zImage,和initrd 、 initramfs,以及SystemV 和 SystemD

目录 一、vmlinux、vmlinuz、zImage、bzImage、uImage 二、initrd 和 initramfs 1、initrd&#xff08;Initial RAM Disk&#xff09; 2、initramfs&#xff08;Initial RAM Filesystem&#xff09; 3、initrd vs. initramfs 对比 4. 如何查看和生成 initramfs 三、Syste…

AIStarter Windows 版本迎来重磅更新!模型插件工作流上线,支持 Ollama / ComfyUI 等多平台本地部署模型统一管理

如果你正在使用 AIStarter 工具进行本地 AI 模型部署 &#xff0c;那么这条消息对你来说非常重要&#xff01; 在最新推出的 AIStarter Windows 正式版更新中 &#xff0c;官方对整个平台进行了功能重构和性能优化&#xff0c;尤其是新增了「模型插件工作流 」功能&#xff0c…

深入理解桥接模式:解耦抽象与实现的设计艺术

一、为什么需要桥接模式&#xff1f;从“类爆炸”问题说起 你是否遇到过这样的开发困境&#xff1f; 当需要为系统扩展新功能时&#xff0c;继承体系像滚雪球一样越变越臃肿&#xff1a;新增一种遥控器类型&#xff0c;需要为电视、音响各写一个子类&#xff1b;新增一种设备类…

Java 中的泛型原理与实践案例

引言&#xff1a;为什么需要泛型 在Java 5之前&#xff0c;集合类只能存储Object类型的对象&#xff0c;这带来了两个主要问题&#xff1a; 类型不安全&#xff1a;可以向集合中添加任何类型的对象&#xff0c;容易出错繁琐的类型转换&#xff1a;从集合中取出元素时需要手动…

springboot3+vue3融合项目实战-大事件文章管理系统-获取文章分类详情

GetMapping("/detail")public Result<Category> detail(Integer id){Category c categoryService.findById(id);return Result.success(c);}在CategoryService接口增加 Category findById(Integer id); 在CategoryServiceImpl增加 Overridepublic Category f…

从零开始创建一个 Next.js 项目并实现一个 TodoList 示例

Next.js 是一个基于 React 的服务端渲染框架&#xff0c;它提供了很多开箱即用的功能&#xff0c;如自动路由、API 路由、静态生成、增量静态再生等。本文将带你一步步创建一个 Next.js 项目&#xff0c;并实现一个简单的 TodoList 功能。 效果地址 &#x1f9f1; 安装 Next.j…

分布式锁: Redisson红锁(RedLock)原理与实现细节

分布式锁是分布式系统的核心基础设施&#xff0c;但 单节点 Redis 锁在高可用场景下存在致命缺陷&#xff1a;当 Redis 主节点宕机时&#xff0c;从节点可能因异步复制未完成而丢失锁信息&#xff0c;导致多个客户端同时持有锁。为此&#xff0c;Redis 作者 Antirez 提出了 Red…

c++多态面试题之(析构函数与虚函数)

有以下问题展开 析构函数要不要定义成虚函数&#xff1f;基类的析构函数要不要定义成虚函数&#xff1f;如果不定义会有什么问题&#xff0c;定义了在什么场景下起作用。 1. 基类析构函数何时必须定义为虚函数&#xff1f; 当且仅当通过基类指针&#xff08;或引用&#xff09;…

Python高级进阶:Vim与Vi使用指南

李升伟 整理 在 Python 高级进阶中&#xff0c;使用 Vim 或 Vi 作为代码编辑器可以显著提升开发效率&#xff0c;尤其是在远程服务器开发或快速脚本编辑时。以下是关于它们在 Python 开发中的高级应用详解&#xff1a; 1. Vim/Vi 简介 Vi&#xff1a;经典的 Unix 文本编辑器…

Dify中使用插件LocalAI配置模型供应商报错

服务器使用vllm运行大模型&#xff0c;今天在Dify中使用插件LocalAI配置模型供应商后&#xff0c;使用工作流的时候&#xff0c;报错&#xff1a;“Run failed: PluginInvokeError: {"args":{},"error_type":"ValueError","message":&…

深度学习驱动下的目标检测技术:原理、算法与应用创新(二)

三、主流深度学习目标检测算法剖析 3.1 R - CNN 系列算法 3.1.1 R - CNN 算法详解 R - CNN&#xff08;Region - based Convolutional Neural Networks&#xff09;是将卷积神经网络&#xff08;CNN&#xff09;应用于目标检测领域的开创性算法&#xff0c;其在目标检测发展历…

【Umi】项目初始化配置和用户权限

app.tsx import { RunTimeLayoutConfig } from umijs/max; import { history, RequestConfig } from umi; import { getCurrentUser } from ./services/auth; import { message } from antd;// 获取用户信息 export async function getInitialState(): Promise<{currentUse…

[学习] RTKLib详解:qzslex.c、rcvraw.c与solution.c

RTKLib详解&#xff1a;qzslex.c、rcvraw.c与solution.c 本文是 RTKLlib详解 系列文章的一篇&#xff0c;目前该系列文章还在持续总结写作中&#xff0c;以发表的如下&#xff0c;有兴趣的可以翻阅。 [学习] RTKlib详解&#xff1a;功能、工具与源码结构解析 [学习]RTKLib详解…

移植RTOS,发现任务栈溢出怎么办?

目录 1、硬件检测方法 2、软件检测方法 3、预防堆栈溢出 4、处理堆栈溢出 在嵌入式系统中&#xff0c;RTOS通过管理多个任务来满足严格的时序要求。任务堆栈管理是RTOS开发中的关键环节&#xff0c;尤其是在将RTOS移植到新硬件平台时。堆栈溢出是嵌入式开发中常见的错误&am…

window 显示驱动开发-使用有保证的协定 DMA 缓冲区模型

Windows Vista 的显示驱动程序模型保证呈现设备的 DMA 缓冲区和修补程序位置列表的大小。 修补程序位置列表包含 DMA 缓冲区中命令引用的资源的物理内存地址。 在有保证的协定模式下&#xff0c;用户模式显示驱动程序知道 DMA 缓冲区和修补程序位置列表的确切大小&#xff0c;…

SD-HOST Controller design-----SD CLK 设计

hclk的分频电路&#xff0c;得到的分频时钟作为sd卡时钟。 该模块最终输出两个时钟&#xff1a;一个为fifo_sd_clk,另一个为out_sd_clk_dft。当不分频时&#xff0c;fifo_sd_clk等于hclk&#xff1b;当分频时候&#xff0c;div_counter开始计数&#xff0c;记到相应分频的时候…

完全背包问题中「排列数」与「组合数」的核心区别

&#x1f3af; 一句话理解 求组合数&#xff08;不计顺序&#xff09; → 外层遍历物品&#xff0c;内层遍历背包容量 求排列数&#xff08;计顺序&#xff09; → 外层遍历背包容量&#xff0c;内层遍历物品 &#x1f3b2; 举例说明 假设有硬币 [1, 2, 3]&#xff0c;目标金…

NHANES指标推荐:MDS

文章题目&#xff1a;The association between magnesium depletion score (MDS) and overactive bladder (OAB) among the U.S. population DOI&#xff1a;10.1186/s41043-025-00846-x 中文标题&#xff1a;美国人群镁耗竭评分 &#xff08;MDS&#xff09; 与膀胱过度活动症…

C++:字符串操作函数

strcpy() 功能&#xff1a;把一个字符串复制到另一个字符串。 #include <iostream> #include <cstring> using namespace std;int main() {char src[] "Hello";char dest[10];strcpy(dest, src);cout << "Copied string: " << …

1基·2台·3空间·6主体——蓝象智联解码可信数据空间的“数智密码”

近日&#xff0c;由全国数据标准化技术委员会编制的《可信数据空间 技术架构》技术文件正式发布&#xff0c;标志着我国数据要素流通体系向标准化、规范化迈出关键一步。该文件从技术功能、业务流程、安全要求三大维度对可信数据空间进行系统性规范&#xff0c;为地方、行业及企…