欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#中的圖像Image類與打印Printing類用法

 更新時間:2022年05月16日 08:16:53   作者:springsnow  
這篇文章介紹了C#中圖像Image類與打印Printing類的用法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

一、Images

1、概述

Image 類為Bitmap(位圖) 和 Metafile(矢量圖) 的類提供功能的抽象基類。Image類不能直接創(chuàng)建對象的,但Image.FromFile()返回的是Bitmap或者Metafile的對象。

初始化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):旋轉、翻轉或者同時旋轉和翻轉 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、旋轉

Image img = Bitmap.FromFile(@"C:\music.bmp");
PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
PictureBox1.Image = img;
//旋轉
img.RotateFlip(RotateFlipType.Rotate180FlipY);
PictureBox1.Image = img;

8、雙倍緩沖

//創(chuàng)建一個與窗體工作區(qū)大小相同的空位圖
using (Bitmap image = new Bitmap(ClientRectangle.Width, ClientRectangle.Height))//創(chuàng)建位圖實例
{
    Graphics g = Graphics.FromImage(image);//以此圖像做畫布,畫圖形
    g.FillRectangle(Brushes.White, ClientRectangle);
    g.DrawImage(image, ClientRectangle);  //在窗體中繪制出內存中的圖像
}

9、格式轉換與保存:

img.Save("c:/1.jpg", ImageFormat.Jpeg);
img.Save("c:/1.gif", ImageFormat.Gif);

二、打印 System.Drawing.Printing

1、打印預覽

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";       //打印機名稱
pd.DefaultPageSettings.Landscape = true;  //設置橫向打印,不設置默認是縱向的
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
pd.Print();

3、分頁打印文本文件

多頁打印必須把HasMorePages 設為true,達到需要的頁數(shù)后關掉此屬性。否則無窮添加新頁面!

當HasMorePages 設為true后,PrintDocument_PrintPage重復自我運行,直到HasMorePages 設為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;
    }
}

到此這篇關于C#中圖像Image類與打印Printing類的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論