網(wǎng)站首頁 編程語言 正文
一、Images
1、概述
Image 類為Bitmap(位圖) 和 Metafile(矢量圖) 的類提供功能的抽象基類。Image類不能直接創(chuàng)建對(duì)象的,但I(xiàn)mage.FromFile()返回的是Bitmap或者M(jìn)etafile的對(duì)象。
初始化Image:
Image img0 = Image.FromFile(@"C:\1.jpg");
Image img1 = Image.FromStream(File.OpenRead(@"C:\1.jpg"));
Image img2 = new Bitmap(@"C:\1.jpg");
2、屬性
- PixelFormat:獲取此 Image 的像素格式。
- RawFormat:獲取此 Image 的文件格式。
- Size:獲取此圖像的寬度和高度(以像素為單位)。
- Width:獲取此 Image 的寬度(以像素為單位)。
- Height:獲取此 Image 的高度(以像素為單位)。
3、方法
- FromFile(String):從指定的文件創(chuàng)建 Image。
- FromStream(Stream):從指定的數(shù)據(jù)流創(chuàng)建 Image。
- GetBounds(GraphicsUnit):以指定的單位獲取圖像的界限。
- GetThumbnailImage(Int32, Int32, Image+GetThumbnailImageAbort, IntPtr):返回此 Image 的縮略圖。
- RotateFlip(RotateFlipType):旋轉(zhuǎn)、翻轉(zhuǎn)或者同時(shí)旋轉(zhuǎn)和翻轉(zhuǎn) Image。
- Save(Stream, ImageFormat):將此圖像以指定的格式保存到指定的流中。
- Save(String, ImageFormat):將此 Image 以指定格式保存到指定文件。
4、繪制圖片:
using (Image img = new Bitmap(@"C:\1.jpg"))
{
System.Drawing.Graphics g = Graphics.FromImage(img);
g.DrawImage(img, new System.Drawing.Rectangle(0, 0, img.Width, img.Height));
}
5、縮放:
Image img1 = new Bitmap(@"C:\1.jpg");
using (Image smallImage = new Bitmap(img1, new Size(img1.Width / 2, img1.Height / 2)))
{
//...
}
6、獲取縮略圖
Bitmap myBitmap = new Bitmap("Climber.jpg");
Image myThumbnail = myBitmap.GetThumbnailImage(40, 40, null, IntPtr.Zero);
e.Graphics.DrawImage(myThumbnail, 150, 75);
7、旋轉(zhuǎn)
Image img = Bitmap.FromFile(@"C:\music.bmp");
PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
PictureBox1.Image = img;
//旋轉(zhuǎn)
img.RotateFlip(RotateFlipType.Rotate180FlipY);
PictureBox1.Image = img;
8、雙倍緩沖
//創(chuàng)建一個(gè)與窗體工作區(qū)大小相同的空位圖
using (Bitmap image = new Bitmap(ClientRectangle.Width, ClientRectangle.Height))//創(chuàng)建位圖實(shí)例
{
Graphics g = Graphics.FromImage(image);//以此圖像做畫布,畫圖形
g.FillRectangle(Brushes.White, ClientRectangle);
g.DrawImage(image, ClientRectangle); //在窗體中繪制出內(nèi)存中的圖像
}
9、格式轉(zhuǎn)換與保存:
img.Save("c:/1.jpg", ImageFormat.Jpeg);
img.Save("c:/1.gif", ImageFormat.Gif);
二、打印 System.Drawing.Printing
1、打印預(yù)覽
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
PrintPreviewDialog ppd = new PrintPreviewDialog();
ppd.Document = pd;
ppd.ShowDialog();
2、打印
PrintDocument pd = new PrintDocument();
pd.DefaultPageSettings.PrinterSettings.PrinterName = "ZDesigner GX430t"; //打印機(jī)名稱
pd.DefaultPageSettings.Landscape = true; //設(shè)置橫向打印,不設(shè)置默認(rèn)是縱向的
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
pd.Print();
3、分頁打印文本文件
多頁打印必須把HasMorePages 設(shè)為true,達(dá)到需要的頁數(shù)后關(guān)掉此屬性。否則無窮添加新頁面!
當(dāng)HasMorePages 設(shè)為true后,PrintDocument_PrintPage重復(fù)自我運(yùn)行,直到HasMorePages 設(shè)為false。
private string text = string.Empty;
private int top = 0;
private Size textSize = Size.Empty;
private void button1_Click(object sender, EventArgs e)
{
text = this.textBox1.Text;
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
pd.BeginPrint += new PrintEventHandler(pd_BeginPrint);
using (System.Windows.Forms.PrintPreviewDialog dlg = new PrintPreviewDialog())
{
dlg.Document = pd;
dlg.WindowState = FormWindowState.Maximized;
dlg.AllowTransparency = true;
dlg.AutoScaleMode = AutoScaleMode.Dpi;
dlg.ShowDialog();
}
}
private void pd_BeginPrint(object sender, PrintEventArgs e)
{
textSize = Size.Empty;
top = 0;
}
private void pd_PrintPage(object sender, PrintPageEventArgs e)
{
if (textSize == Size.Empty)
{
textSize = Size.Round(e.Graphics.MeasureString(text, Font, e.MarginBounds.Width));
}
e.Graphics.SetClip(e.MarginBounds);
e.Graphics.DrawString(text, this.Font, Brushes.Black, new Rectangle(e.MarginBounds.Location.X, e.MarginBounds.Location.Y + top * -1, textSize.Width, textSize.Height)); ;
if (top + e.MarginBounds.Height < textSize.Height)
{
top = top + e.MarginBounds.Height;
e.HasMorePages = true;
}
}
原文鏈接:https://www.cnblogs.com/springsnow/p/9433976.html
相關(guān)推薦
- 2022-07-18 進(jìn)程和計(jì)劃任務(wù)管理
- 2022-07-17 C#編程報(bào)錯(cuò)System.InvalidOperationException問題及解決_C#教程
- 2024-07-15 Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SDS)
- 2022-10-17 Android數(shù)據(jù)存儲(chǔ)方式操作模式解析_Android
- 2023-06-21 Android面向單Activity開發(fā)示例解析_Android
- 2022-05-10 用async修飾的函數(shù)是異步函數(shù)嗎?
- 2023-01-02 Python創(chuàng)建相同值數(shù)組/列表的兩種方法_python
- 2022-08-10 pandas中read_sql使用參數(shù)進(jìn)行數(shù)據(jù)查詢的實(shí)現(xiàn)_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支