C# 图像处理
2023-09-11 14:16:46 时间
一 GDI+中的图像处理
1 GDI+中对图像处理提供了以下支持:
① 支持BMP、GIF、JPEG、PNG、TIFF、ICON等等广泛格式的图像文件;
② 提供了用于多种光栅图像格式进行编码和解码的公共接口;
③ 支持为图像格式添加动态格式;
④ 支持对图像的像素进行多种处理,包括亮度,对比度,颜色平滑、模糊;
⑤ 支持对图像进行选择、剪切等操作;
2 主要公共Image实现
二 Bitmap类
1 Image是抽象类,Bitmap从Image派生
2 可以出来BMP、Jpeg、GIF、PNG等格式
3 构建
① Bitmap bt1=new Bitmap(“c:\1.bmp”);
② Bitmap bt2=new Bitmap(bt1,200,300);
③ Bitmap bt3;
bt3.FromFile(“文件名称”);
三 图像的绘制
整个图像的绘制
DrawImage;
四 图像处理
1 图像文件bmp格式;
2 使用bitmap.GetPixel(x,y);得到像素点;
2 使用指针
bitmapData=bitmap.LockBits(bounds,ImageLockMode.ReadWrite,PixelForm.Format24bppRgb);
pBase=(Byte*)bitmapData.Scan0.ToPointer();
(PixelData*)(pBase+y*stride+x*sizeof(PixelData));
3 因为使用unsafe,所以编译的时候需要设置“允许不安全的代码”.
五 图像过滤
图像过滤
针对像素的运算
更全面的图像过滤器.rar: https://url09.ctfile.com/f/22158009-749685290-1cc953?p=5939 (访问密码: 5939)
生产缩略图
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
class ThumbnailTest
{
static void Main()
{
string file = @"c:\1.jpg";
Bitmap bitmap = new Bitmap( file );
Bitmap thum = CreateThumbnail( bitmap, 100, 80 );
thum.Save( @"c:\1_small.jpg");
}
public static Bitmap CreateThumbnail(Bitmap originalBmp, int desiredWidth, int desiredHeight)
{
// If the image is smaller than a thumbnail just return it
if (originalBmp.Width <= desiredWidth && originalBmp.Height <= desiredHeight)
{
return originalBmp;
}
int newWidth, newHeight;
// scale down the smaller dimension
if (desiredWidth * originalBmp.Height < desiredHeight * originalBmp.Width)
{
newWidth = desiredWidth;
newHeight = (int)Math.Round((decimal)originalBmp.Height * desiredWidth / originalBmp.Width);
}
else
{
newHeight = desiredHeight;
newWidth = (int)Math.Round((decimal)originalBmp.Width * desiredHeight / originalBmp.Height);
}
// This code creates cleaner (though bigger) thumbnails and properly
// and handles GIF files better by generating a white background for
// transparent images (as opposed to black)
// This is preferred to calling Bitmap.GetThumbnailImage()
Bitmap bmpOut = new Bitmap(newWidth, newHeight);
using (Graphics graphics = Graphics.FromImage(bmpOut))
{
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.FillRectangle(Brushes.White, 0, 0, newWidth, newHeight);
graphics.DrawImage(originalBmp, 0, 0, newWidth, newHeight);
}
return bmpOut;
}
}
相关文章
- C# EntityFramework Code First 迁移 降级 回退到空数据库
- C#如何发布项目 发布软件
- C# 使用 MemoryStream 将数据写入内存
- C#中巧用#if DEBUG 进行调试
- c# 如何在一般处理程序中返回json
- 数字图像处理 使用C#进行图像处理三
- 数字图像处理 使用C#进行图像处理四 边缘检测
- 数字图像处理 使用C#进行图像处理五 访问像素的三种方式
- 数字图像处理 使用C#进行图像处理六 内存映射文件
- C# 字符串操作--减少垃圾回收压力
- C# dev gridcontrol “时间”字符串格式化
- C# XML本地文件转换成XML字符串
- Atitit. c# 语法新特性 c#2.0 3.0 4.0 4.5 5.0 6.0 attilax总结 1. 版本历史1 1.1. C# 1.0-纯粹的面向对象2 1.2. C# 2.0
- Atitit.http httpclient实践java c# .net php attilax总结
- C#与Matlab混合编程之巴特沃斯低通滤波器(转)
- C# 绘图窗体(直线,圆,矩形,橡皮擦)可以调节画布颜色,粗细)还有图像处理功能
- C#读写锁ReaderWriterLockSlim的使用
- WinFrom、C# 学习记录一
- C# winform 文件批量拷贝到目标文件夹【案例+源码】