C# WinForm 使用多种方法实现 图片的切割和拼接
2012-02-04 23:17
926 查看
1、通过拷贝像素 实现图片的切割和拼接
来自:百度空间http://hi.baidu.com/%B2%E8%B6%E0%B7%D3%BB%BC%D5%DF/blog/item/051081c7df4c5c069c163da6.html这种方法我不想试了,效率太低了
try
{
int count = myarray.Count;//动态数据,保存所有图片的Top和Left,以及最大点的坐标 int width = (((int)myarray[count - 2])/256+1)*256;//使动态生成的图片长和宽式256的整数倍
int height = (((int)myarray[count - 1])/256+1)*256;
Bitmap newBitmap = new Bitmap(width,height);
Color pixel;
int width1 = 0, height1 = 0;
for (int j = 0; j <(count-2)/2; j++)
{
Bitmap oldBitmap=(Bitmap)((PictureBox)this.Controls.Find("newpicturebox"+j, true)[0]).Image;
width1 = ((PictureBox)this.Controls.Find("newpicturebox"+j, true)[0]).Image.Width;
height1 = ((PictureBox)this.Controls.Find("newpicturebox"+j, true)[0]).Image.Height;
int px =Convert.ToInt16(myarray[j*2]);// ((PictureBox)this.Controls.Find("newpicturebox" + j, true)[0]).Location.X;
int py=Convert.ToInt16(myarray[j*2+1]);//((PictureBox)this.Controls.Find("newpicturebox"+j, true)[0]).Location.Y;
for(int p=0;p<width1;p++)
for (int q=0; q<height1; q++)
{
pixel = oldBitmap.GetPixel(p,q);
newBitmap.SetPixel(px+p,py+q, pixel);
}
}
Clear();
this.pictureBox1.Size = new Size(width,height);
this.pictureBox1.Image = newBitmap;
newBitmap.Save(DirectoryInfoPath+"00.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "信息提示");
} 图片切割: int width2 = this.pictureBox1.Width;
int height2 = this.pictureBox1.Height;
Bitmap newBitmap = new Bitmap(256, 256);
Bitmap oldBitmap = (Bitmap)this.pictureBox1.Image;
Color pixel;
for (int i = 0; i <width2 / 256; i++)
for (int j = 0; j < height2 / 256; j++)
{
for (int p =i * 256; p < i * 256 + 256; p++)
for (int q = i * 256; q < j * 256 + 256; q++)
{
pixel = oldBitmap.GetPixel(p, q);
newBitmap.SetPixel(p%256,q%256, pixel);
}
newBitmap.Save(DirectoryInfoPath +"新"+i+j+ ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
2、使用Graphics类中的DrawImage方法,实现图片的拼接Graphics draw;
来自:博客园http://www.cnblogs.com/youwang/archive/2011/12/22/2298594.htmlprivate Image JoinImage(List imageList, JoinMode jm) { //图片列表 if (imageList.Count <= 0) return null; if (jm == JoinMode.Horizontal) { //横向拼接 int width = 0; //计算总长度 foreach (Image i in imageList) { width += i.Width; } //高度不变 int height = imageList.Max(x => x.Height); //构造最终的图片白板 Bitmap tableChartImage = new Bitmap(width, height); Graphics graph = Graphics.FromImage(tableChartImage); //初始化这个大图 graph.DrawImage(tableChartImage, width, height); //初始化当前宽 int currentWidth = 0; foreach (Image i in imageList) { //拼图 graph.DrawImage(i, currentWidth, 0); //拼接改图后,当前宽度 currentWidth += i.Width; } return tableChartImage; } else if (jm == JoinMode.Vertical) { //纵向拼接 int height = 0; //计算总长度 foreach (Image i in imageList) { height += i.Height; } //宽度不变 int width = imageList.Max(x => x.Width); //构造最终的图片白板 Bitmap tableChartImage = new Bitmap(width, height); Graphics graph = Graphics.FromImage(tableChartImage); //初始化这个大图 graph.DrawImage(tableChartImage, width, height); //初始化当前宽 int currentHeight = 0; foreach (Image i in imageList) { //拼图 graph.DrawImage(i, 0, currentHeight); //拼接改图后,当前宽度 currentHeight += i.Height; } return tableChartImage; } else { return null; } }也做了一个很简陋的图像界面,如下图所示:
3、使用[API-BitBlt]
(1)、声明API#region Win32API [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")] private static extern bool BitBlt( IntPtr hdcDest, // 目标 DC的句柄 int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, // 源DC的句柄 int nXSrc, int nYSrc, System.Int32 dwRop // 光栅的处理数值 ); [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")] private static extern bool DeleteObject(IntPtr hdc); [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")] private static extern IntPtr SelectObject(IntPtr hdc, IntPtr hObject); #endregion
(2)、函数
const int SRCCOPY = 0x00CC0020; public bool CutPictrueToStream(Bitmap BmpSource) { Graphics grSource = Graphics.FromImage(BmpSource); Bitmap Bitmap_cutted = new Bitmap(SizeX, SizeY, grSource); IntPtr hdcTarget = IntPtr.Zero; IntPtr hdcSource = IntPtr.Zero; IntPtr hBitmapSource = IntPtr.Zero; IntPtr hOldObject = IntPtr.Zero; hBitmapSource = BmpSource.GetHbitmap(); MemorySource = new MemoryStream[ClassCount][][]; for (int i = 0; i < ClassCount; i++) { MemorySource[i] = new MemoryStream[DirectionCount][]; for (int j = 0; j < DirectionCount; j++) { MemorySource[i][j] = new MemoryStream[Frames[i]]; for (int k = 0; k < Frames[i]; k++) { Graphics grTarget = Graphics.FromImage(Bitmap_cutted); hdcTarget = grTarget.GetHdc(); hdcSource = grSource.GetHdc(); hOldObject = SelectObject(hdcSource, hBitmapSource); BitBlt(hdcTarget, 0, 0, SizeX, SizeY, hdcSource, (i * Frames[i] + k) * SizeX, j * SizeY, SRCCOPY); //必须释放DC,否则保存为黑图 if (hdcTarget != IntPtr.Zero) { grTarget.ReleaseHdc(hdcTarget); } if (hdcSource != IntPtr.Zero) { grSource.ReleaseHdc(hdcSource); } Bitmap_cutted.MakeTransparent();//保存为透明背景 Bitmap_cutted.Save(@"F:\Project\VS 2008\C#\(十一)地图遮罩层的实现\(十一)地图遮罩层的实现\Player\" + i.ToString() + "_" + j.ToString() + "_" + k.ToString() + ".Png",ImageFormat.Png); // MemorySource[i][j][k] = new MemoryStream(); // Bitmap_cutted.Save(MemorySource[i][j][k], ImageFormat.Png); grTarget.Dispose(); } } } if (hOldObject != IntPtr.Zero) SelectObject(hdcSource, hOldObject); if (hBitmapSource != IntPtr.Zero) DeleteObject(hBitmapSource); grSource.Dispose(); Bitmap_cutted.Dispose(); return true; }
(3)效果
相关文章推荐
- C#使用Socket实现发送和接收图片的方法
- C#在WinForm中使用WebKit传递js对象实现与网页交互的方法
- C#实现winform中RichTextBox在指定光标位置插入图片的方法
- C#在WinForm中使用WebKit传递js对象实现与网页交互的方法
- sql server 关于表中只增标识问题 C# 实现自动化打开和关闭可执行文件(或 关闭停止与系统交互的可执行文件) ajaxfileupload插件上传图片功能,用MVC和aspx做后台各写了一个案例 将小写阿拉伯数字转换成大写的汉字, C# WinForm 中英文实现, 国际化实现的简单方法 ASP.NET Core 2 学习笔记(六)ASP.NET Core 2 学习笔记(三)
- C# Winform 使用Adobe Acrobat 实现 PDF 转 图片
- C#实现图片切割的方法
- C#实现Winform间的数据交互的三种方法
- C#实现动态显示及动态移除图片方法
- C#中使用委托、接口、匿名方法、泛型委托实现加减乘除算法
- 多种方法实现H5网页GIF图片动画效果
- C#构建多线程应用程序(1) —— 使用委托来自动创建次线程以实现异步方法的调用
- C#实现缩放和剪裁图片的方法示例
- C#实现图片上传与浏览切换的方法
- 常量,字段,构造方法 调试 ms 源代码 一个C#二维码图片识别的Demo 近期ASP.NET问题汇总及对应的解决办法 c# chart控件柱状图,改变柱子宽度 使用C#创建Windows服务 C#服务端判断客户端socket是否已断开的方法 线程 线程池 Task .NET 单元测试的利剑——模拟框架Moq
- (转)判断一个字符串是否全是数字的多种方法及其性能比较(C#实现
- [导入]Apache服务器使用.htaccess实现图片防盗链方法教程
- 在C#中使用异步Socket编程实现TCP网络服务的C/S的通讯构架(二)----使用方法
- C#使用SendMessage实现进程间通信的方法
- qt button以及label实现不规则图形(五种方法:使用QSS,设置Mask图片,自己画)