private void mirror_Click(object sender, EventArgs e)
{
if (curBitmap!=null)
{
mirror mirForm = new mirror();
if (mirForm.ShowDialog()==DialogResult.OK)
{
Rectangle rect = new Rectangle(, , curBitmap.Width, curBitmap.Height);
BitmapData bmpData = curBitmap.LockBits(rect, ImageLockMode.ReadWrite, curBitmap.PixelFormat);
IntPtr ptr = bmpData.Scan0;
int bytes = ;
////判断是灰度色图像还是彩色图像,给相应的大小
if (curBitmap.PixelFormat==PixelFormat.Format8bppIndexed)
{
bytes= curBitmap.Width * curBitmap.Height;
}
else if (curBitmap.PixelFormat == PixelFormat.Format24bppRgb)
{
bytes = curBitmap.Width * curBitmap.Height * ;
}
byte[] pixelValues = new byte[bytes];
Marshal.Copy(ptr, pixelValues, , bytes); //水平中轴
int halfWidth = curBitmap.Width / ;
//垂直中轴
int halfHeight = curBitmap.Height / ;
byte temp;
byte temp1;
byte temp2;
byte temp3;
if (curBitmap.PixelFormat == PixelFormat.Format8bppIndexed)
{
if (mirForm.GetMirror)
{
for (int i = ; i < curBitmap.Height; i++)
for (int j = ; j < halfWidth; j++)
{
temp = pixelValues[i * curBitmap.Width + j];
pixelValues[i * curBitmap.Width + j] = pixelValues[(i + ) * curBitmap.Width - j - ];
pixelValues[(i + ) * curBitmap.Width - j - ] = temp;
}
}
else
{
for (int j = ; j < curBitmap.Width; j++)
{
for (int i = ; i < halfHeight; i++)
{
temp = pixelValues[i * curBitmap.Width + j];
pixelValues[i * curBitmap.Width + j] = pixelValues[(curBitmap.Height - i - ) * curBitmap.Width + j];
pixelValues[(curBitmap.Height - i - ) * curBitmap.Width + j] = temp;
}
}
}
}
else if (curBitmap.PixelFormat == PixelFormat.Format24bppRgb)
{
if (mirForm.GetMirror)
{
//水平镜像处理
for (int i = ; i < curBitmap.Height; i++)
{
//每个像素的三个字节在水平镜像时顺序不能变,所以这个方法不能用
//for (int j = 0; j < halfWidth; j++)
//{
// //以水平中轴线为对称轴,两边像素值交换
// temp = pixelValues[i * curBitmap.Width * 3 + j * 3];
// pixelValues[i * curBitmap.Width * 3 + j * 3] = pixelValues[(i + 1) * curBitmap.Width * 3 - 1 - j * 3];
// pixelValues[(i + 1) * curBitmap.Width * 3 - 1 - j * 3] = temp;
//}
for (int j = ; j < halfWidth; j++)
{//每三个字节组成一个像素,顺序不能乱
temp = pixelValues[ + i * curBitmap.Width * + j * ];
temp1 = pixelValues[ + i * curBitmap.Width * + j * ];
temp2 = pixelValues[ + i * curBitmap.Width * + j * ];
pixelValues[ + i * curBitmap.Width * + j * ] = pixelValues[ + (i + ) * curBitmap.Width * - (j + ) * ];
pixelValues[ + i * curBitmap.Width * + j * ] = pixelValues[ + (i + ) * curBitmap.Width * - (j + ) * ];
pixelValues[ + i * curBitmap.Width * + j * ] = pixelValues[ + (i + ) * curBitmap.Width * - (j + ) * ];
pixelValues[ + (i + ) * curBitmap.Width * - (j + ) * ] = temp;
pixelValues[ + (i + ) * curBitmap.Width * - (j + ) * ] = temp1;
pixelValues[ + (i + ) * curBitmap.Width * - (j + ) * ] = temp2;
}
}
}
else
{
//垂直镜像处理
for (int i = ; i < curBitmap.Width * ; i++)
{
for (int j = ; j < halfHeight; j++)
{
//以垂直中轴线为对称轴。两边像素值互换
temp = pixelValues[j * curBitmap.Width * + i];
pixelValues[j * curBitmap.Width * + i] = pixelValues[(curBitmap.Height - j - ) * curBitmap.Width * + i];
pixelValues[(curBitmap.Height - j - ) * curBitmap.Width * + i] = temp;
}
}
}
} Marshal.Copy(pixelValues, , ptr, bytes);
curBitmap.UnlockBits(bmpData);
}
Invalidate();
}
}

原图:

水平镜像:

垂直镜像:

c#数字图像处理(九)图像镜像的更多相关文章

  1. Win8 Metro(C#) 数字图像处理--1 图像打开,保存

    原文:Win8 Metro(C#) 数字图像处理--1 图像打开,保存 作为本专栏的第一篇,必不可少的需要介绍一下图像的打开与保存,一便大家后面DEMO的制作.   Win8Metro编程中,图像相关 ...

  2. Win8 Metro(C#)数字图像处理--4图像颜色空间描述

    原文:Win8 Metro(C#)数字图像处理--4图像颜色空间描述  图像颜色空间是图像颜色集合的数学表示,本小节将针对几种常见颜色空间做个简单介绍. /// <summary> / ...

  3. 数字图像处理,图像锐化算法的C++实现

    http://blog.csdn.net/ebowtang/article/details/38961399 之前一段我们提到的算法都是和平滑有关, 经过平滑算法之后, 图像锐度降低, 降低到一定程度 ...

  4. 数字图像处理:图像的灰度变换(Matlab实现)

    (1)线性变换:通过建立灰度映射来调整源图像的灰度. k>1增强图像的对比度:k=1调节图像亮度,通过改变d值达到调节亮度目的:0 i = imread('theatre.jpg');i = i ...

  5. 数字图像处理界标准图像 Lena 后面的故事

    熟悉图像处理或者压缩的工程师.研究人员和学生,经常在他们的实验或者项目任务里使用"Lenna"或者"Lena"的图像.Lenna 图像已经成为被广泛使用的测试图 ...

  6. 【数字图像处理】六.MFC空间几何变换之图像平移、镜像、旋转、缩放具体解释

    本文主要讲述基于VC++6.0 MFC图像处理的应用知识,主要结合自己大三所学课程<数字图像处理>及课件进行解说,主要通过MFC单文档视图实现显示BMP图片空间几何变换.包含图像平移.图形 ...

  7. Win8Metro(C#)数字图像处理--2.19图像水平镜像

    原文:Win8Metro(C#)数字图像处理--2.19图像水平镜像  [函数名称] 图像水平镜像函数MirrorXProcess(WriteableBitmap src) [函数代码]      ...

  8. Win8Metro(C#)数字图像处理--2.20图像垂直镜像

    原文:Win8Metro(C#)数字图像处理--2.20图像垂直镜像  [函数名称] 图像垂直镜像函数MirrorYProcess(WriteableBitmap src) [函数代码]      ...

  9. 【数字图像处理】五.MFC图像点运算之灰度线性变化、灰度非线性变化、阈值化和均衡化处理具体解释

    本文主要讲述基于VC++6.0 MFC图像处理的应用知识,主要结合自己大三所学课程<数字图像处理>及课件进行解说.主要通过MFC单文档视图实现显示BMP图片点运算处理.包含图像灰度线性变换 ...

  10. Win8 Metro(C#)数字图像处理--3.2图像方差计算

    原文:Win8 Metro(C#)数字图像处理--3.2图像方差计算 /// <summary> /// /// </summary>Variance computing. / ...

随机推荐

  1. nginx负载均衡的几种模式

    nginx 的 upstream目前支持 4 种方式的分配 ).轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除. ).weight 指定轮询几率,we ...

  2. Python15_包的安装和管理

    pip的安装及简单使用:https://www.cnblogs.com/csucat/p/4897695.html 使用pip手动安装第三方库:https://blog.csdn.net/github ...

  3. QString 转换为 char *

    一.QString 转换为 char * 将 QString 转 char *,需要用到 QByteArray 类,QByteArray 类的说明详见 Qt 帮助文档. 因为 char * 最后都有一 ...

  4. 利用OGG进行数据库表的初始化

    OGG初始化 1.停止目标端re1进程 2.配置ei进程(若目标端有ei进程之前投递的文件则先清空) GGSCI (localhost) 1>add extract ei1, sourceist ...

  5. docker 安装 redis5.0.3

    一.拉取官方5.0.3镜像 [root@localhost ~]# docker pull redis:5.0.3 下载完成 [root@localhost ~]# docker pull redis ...

  6. linux 没有音频输出的解决方式

    用户级别的-/.asoundrc 文件. 如果文件不存在,可以手动创建. 其中的各个 ID,请根据实际情况调整: defaults.pcm.card 1 defaults.pcm.device 0 d ...

  7. Git是什么?

    Git是什么? Git 是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目. Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控 ...

  8. DecoratorPattern(装饰器模式)-----Java/.Net

    装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装

  9. 在C#中使用二叉树实时计算海量用户积分排名的实现

    从何说起 前些天和朋友讨论一个问题,他们的应用有几十万会员然后对应有积分,现在想做积分排名的需求,问有没有什么好方案.这个问题也算常见,很多地方都能看到,常规做法一般是数据定时跑批把计算结果到中间表然 ...

  10. 1092 最好吃的月饼 (20分)C语言

    月饼是久负盛名的中国传统糕点之一,自唐朝以来,已经发展出几百品种. 若想评比出一种"最好吃"的月饼,那势必在吃货界引发一场腥风血雨-- 在这里我们用数字说话,给出全国各地各种月饼的 ...