一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - C# - C#實(shí)現(xiàn)圖片切割、切圖、裁剪

C#實(shí)現(xiàn)圖片切割、切圖、裁剪

2022-03-08 14:13霍莉雪特 C#

這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)圖片切割、切圖、裁剪,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了c#實(shí)現(xiàn)圖片切割、切圖的具體代碼,供大家參考,具體內(nèi)容如下

前臺(tái)準(zhǔn)備兩個(gè)image控件。上面是顯示原圖,下面顯示切割后的效果。

?
1
2
3
4
<stackpanel orientation="vertical">
 <image width="450" height="383" source="c:\users\administrator\documents\visual studio 2015\projects\splitpic\splitpic\images\1.jpg"/>
 <image x:name="img" stretch="none" width="450" height="383" />
</stackpanel>

對(duì)應(yīng)的后臺(tái)代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
public partial class mainwindow : window
{
 public mainwindow()
 {
 initializecomponent();
 
 // 設(shè)置原圖
 img.source = new bitmapimage(new uri(@"images/1.jpg", urikind.relative));
 
 // 切割圖片
 imagesource imagesource = img.source;
 bitmap bitmap = systemutils.imagesourcetobitmap(imagesource);
 bitmapsource bitmapsource = systemutils.bitmaptobitmapimage(bitmap);
 bitmapsource newbitmapsource = systemutils.cutimage(bitmapsource, new int32rect(125, 60, 235, 285));
 
 // 使用切割后的圖源
 img.source = newbitmapsource;
 }
 
}
 
 
// 圖像工具類
public static class systemutils
{
 /// <summary>
 /// 切圖
 /// </summary>
 /// <param name="bitmapsource">圖源</param>
 /// <param name="cut">切割區(qū)域</param>
 /// <returns></returns>
 public static bitmapsource cutimage(bitmapsource bitmapsource, int32rect cut)
 {
 //計(jì)算stride
 var stride = bitmapsource.format.bitsperpixel * cut.width / 8;
 //聲明字節(jié)數(shù)組
 byte[] data = new byte[cut.height * stride];
 //調(diào)用copypixels
 bitmapsource.copypixels(cut, data, stride, 0);
 
 return bitmapsource.create(cut.width, cut.height, 0, 0, pixelformats.bgr32, null, data, stride);
 }
 
 // imagesource --> bitmap
 public static system.drawing.bitmap imagesourcetobitmap(imagesource imagesource)
 {
 bitmapsource m = (bitmapsource)imagesource;
 
 system.drawing.bitmap bmp = new system.drawing.bitmap(m.pixelwidth, m.pixelheight, system.drawing.imaging.pixelformat.format32bpppargb);
 
 system.drawing.imaging.bitmapdata data = bmp.lockbits(
 new system.drawing.rectangle(system.drawing.point.empty, bmp.size), system.drawing.imaging.imagelockmode.writeonly, system.drawing.imaging.pixelformat.format32bpppargb);
 
 m.copypixels(int32rect.empty, data.scan0, data.height * data.stride, data.stride); bmp.unlockbits(data);
 
 return bmp;
 }
 
 // bitmap --> bitmapimage
 public static bitmapimage bitmaptobitmapimage(bitmap bitmap)
 {
 using (memorystream stream = new memorystream())
 {
  bitmap.save(stream, imageformat.bmp);
 
  stream.position = 0;
  bitmapimage result = new bitmapimage();
  result.begininit();
  // according to msdn, "the default ondemand cache option retains access to the stream until the image is needed."
  // force the bitmap to load right now so we can dispose the stream.
  result.cacheoption = bitmapcacheoption.onload;
  result.streamsource = stream;
  result.endinit();
  result.freeze();
 
  return result;
 }
 }
}

運(yùn)行后的效果如下:

C#實(shí)現(xiàn)圖片切割、切圖、裁剪

補(bǔ)充:關(guān)于剪裁的位置和區(qū)域的填寫說明,如下圖。

C#實(shí)現(xiàn)圖片切割、切圖、裁剪

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://blog.csdn.net/qq_18995513/article/details/67637521

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲 欧美 国产 综合 播放 | 日本乱人伦中文在线播放 | 免费观看日本视频 | 4hu永久地域网名入口 | 精品国产线拍大陆久久尤物 | 国士李风起全文在线阅读 | 欧美一级精品 | 修修视频在线观看 | tube62hdxxxx日本 | 亚洲天堂精品视频 | 日日摸日日碰夜夜爽97纠 | 亚洲国产成人久久综合区 | 美女靠逼免费视频 | 小舞同人18av黄漫网站 | 大学生宿舍飞机china free | 四虎成人免费 | 久久电影午夜 | 91精品综合久久久久久五月天 | 青青青久热国产精品视频 | 精品99在线观看 | 美女下面揉出水免费视频 | 九九免费精品视频 | 白丝捆绑调教 | 国产精品亚洲专区一区 | 好大~好爽~再进去一点 | 男人午夜禁片在线观看 | 亚洲娇小videos | 欧美多gayxxxx| 高清不卡一区 | 亚洲欧美一级夜夜爽w | 成人精品视频一区二区在线 | 精品91自产拍在线观看99re | 亚洲好色网| 精品一久久香蕉国产二月 | 毛片免费在线视频 | avove本人照片| 日韩精品一区二区三区毛片 | 美女隐私部位视频网站 | 久久久免费观成人影院 | 国产高清在线观看 | 精品国产精品人妻久久无码五月天 |