图像居中CenterImage

来源:互联网 发布:西安云计算 编辑:程序博客网 时间:2024/05/16 06:18

Code:


基类XMarksTheSpot(支持打印):

using System;using System.Windows.Forms;using System.Drawing;using System.Drawing.Printing;using System.Drawing.Drawing2D;namespace CsStudy{    class XMarksTheSpot : Form    {        public static void Main()        {            Application.Run(new XMarksTheSpot());        }        public XMarksTheSpot()        {            Text = this.Name;            ResizeRedraw = true;        }        protected override void OnPaint(PaintEventArgs e)        {            DoPage(e.Graphics, ForeColor, ClientSize.Width, ClientSize.Height);            base.OnPaint(e);        }        protected override void OnClick(EventArgs e)        {            PrintDocument prndoc = new PrintDocument();            prndoc.DocumentName = Text;            prndoc.PrintPage += new PrintPageEventHandler(prndoc_PrintPage);            prndoc.Print();//打印            base.OnClick(e);        }        void prndoc_PrintPage(object obj, PrintPageEventArgs e)        {            Graphics grph = e.Graphics;            SizeF sizef = grph.VisibleClipBounds.Size;            DoPage(grph, Color.Black, (int)sizef.Width, (int)sizef.Height);        }        protected virtual void DoPage(Graphics grph, Color clr, int cx, int cy)        {            //图形保真            grph.SmoothingMode = SmoothingMode.None;            grph.PixelOffsetMode = PixelOffsetMode.Default;        }    }}


子类:

using System;using System.Windows.Forms;using System.Drawing;namespace CsStudy{    class CenterImage : XMarksTheSpot    {        Image image;        new static void Main()        {            Application.Run(new CenterImage());        }        CenterImage()        {            Text = "图像居中";            image = Image.FromFile(@"F:\photo\网络图片\123.jpg");        }        protected override void DoPage(Graphics grph, Color clr, int cx, int cy)        {            grph.DrawImage(image, (cx - image.Width) / 2, (cy - image.Height) / 2,image.Width,image.Height);        }    }}

效果图:


0 0