c#拼图碎片形状_C# WinForm 使用多种方法实现 图片的切割和拼接

本文介绍了使用C#编程语言在WinForm环境下实现图片切割和拼接的三种方法:1) 通过拷贝像素,2) 利用Graphics类的DrawImage方法,3) 使用API BitBlt进行图像处理。详细代码展示了如何操作Bitmap对象和Graphics对象以完成图像的切割和拼接操作。
摘要由CSDN通过智能技术生成

1、通过拷贝像素 实现图片的切割和拼接

这种方法我不想试了,效率太低了

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

{

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

for (int q=0; 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

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; privateImage JoinImage(List imageList, JoinMode jm)

{

//图片列表if(imageList.Count<= 0)

return null;

if(jm== JoinMode.Horizontal)

{

//横向拼接intwidth= 0;

//计算总长度foreach(Image iinimageList)

{

width+= i.Width;

}

//高度不变intheight= imageList.Max(x=> x.Height);

//构造最终的图片白板Bitmap tableChartImage= newBitmap(width, height);

Graphics graph= Graphics.FromImage(tableChartImage);

//初始化这个大图graph.DrawImage(tableChartImage, width, height);

//初始化当前宽intcurrentWidth= 0;

foreach(Image iinimageList)

{

//拼图graph.DrawImage(i, currentWidth, 0);

//拼接改图后,当前宽度currentWidth+= i.Width;

}

returntableChartImage;

}

else if(jm== JoinMode.Vertical)

{

//纵向拼接intheight= 0;

//计算总长度foreach(Image iinimageList)

{

height+= i.Height;

}

//宽度不变intwidth= imageList.Max(x=> x.Width);

//构造最终的图片白板Bitmap tableChartImage= newBitmap(width, height);

Graphics graph= Graphics.FromImage(tableChartImage);

//初始化这个大图graph.DrawImage(tableChartImage, width, height);

//初始化当前宽intcurrentHeight= 0;

foreach(Image iinimageList)

{

//拼图graph.DrawImage(i, 0, currentHeight);

//拼接改图后,当前宽度currentHeight+= i.Height;

}

returntableChartImage;

}

else{

return null;

}

}也做了一个很简陋的图像界面,如下图所示:

3、使用[API-BitBlt]

(1)、声明API#regionWin32API

[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]

private static extern boolBitBlt(

IntPtrhdcDest, // 目标 DC的句柄intnXDest,

intnYDest,

intnWidth,

intnHeight,

IntPtrhdcSrc, // 源DC的句柄intnXSrc,

intnYSrc,

System.Int32dwRop// 光栅的处理数值);

[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]

private static extern boolDeleteObject(IntPtrhdc);

[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]

private static externIntPtrSelectObject(IntPtrhdc, IntPtrhObject);

#endregion

(2)、函数const intSRCCOPY= 0x00CC0020;

public boolCutPictrueToStream(BitmapBmpSource)

{

GraphicsgrSource= Graphics.FromImage(BmpSource);

BitmapBitmap_cutted= newBitmap(SizeX, SizeY, grSource);

IntPtrhdcTarget= IntPtr.Zero;

IntPtrhdcSource= IntPtr.Zero;

IntPtrhBitmapSource= IntPtr.Zero;

IntPtrhOldObject= IntPtr.Zero;

hBitmapSource= BmpSource.GetHbitmap();

MemorySource= newMemoryStream[ClassCount][][];

for(inti= 0; i< ClassCount; i++)

{

MemorySource[i] = newMemoryStream[DirectionCount][];

for(intj= 0; j< DirectionCount; j++)

{

MemorySource[i][j] = newMemoryStream[Frames[i]];

for(intk= 0; k< Frames[i]; k++)

{

GraphicsgrTarget= 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)效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值