基本信息
源码名称:C#GDI及功能扩展
源码大小:0.03M
文件格式:.rar
开发语言:C#
更新时间:2018-09-12
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 1 元 
   源码介绍


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;

namespace DrawDragRect
{
    public struct PaintStruct
    {
        public Graphics dc;
        public bool isPaint;
    };
    public partial class MainForm : CDrawDragRect
                                    //Form
    {
        public int nDblClick;

        Image imgResize;
        Image img;
        public void LoadImage()
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Image Files(*.BMP;*.JPG;*.GIF;*.Tiff)|*.BMP;*.JPG;*.GIF;*.Tiff ";
            if(DialogResult.OK == ofd.ShowDialog())
                img = Image.FromFile(ofd.FileName);
            Invalidate();
            nDblClick = -1;
        }
        public MainForm()
        {
            InitializeComponent();
            Icon = new Icon(GetType(), "Icon1.ico");
            Text = "DrawDragRect API for .NET";
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            //WindowState = FormWindowState.Minimized;
            WindowState = FormWindowState.Normal;
            LoadImage();
        }
        
        public void DrawImage(PaintStruct ps)
        {
            if (img != null)
            {
                if (imgResize != null)
                    imgResize.Dispose();
                if (ClientRectangle.Width > 0 && ClientRectangle.Height > 0)
                {
                    imgResize = new Bitmap(ClientRectangle.Width, ClientRectangle.Height);
                    Graphics dcTmp = Graphics.FromImage(imgResize);
                    dcTmp.DrawImage(img, ClientRectangle);
                    dcTmp.Dispose();

                    if (ps.isPaint)
                    {
                        Graphics dc = ps.dc;
                        dc.DrawImage(imgResize, ClientRectangle);
                    }
                    else
                    {
                        Graphics dc = CreateGraphics();
                        dc.DrawImage(imgResize, ClientRectangle);
                        ReflectedBLockText(dc, new Point(ClientRectangle.Width / 10, ClientRectangle.Height / 4));
                        dc.Dispose();
                    }
                }
            }
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            PaintStruct ps;
            ps.dc = e.Graphics;
            ps.isPaint = true;
            DrawImage(ps);
            ReflectedBLockText(e.Graphics, new Point(ClientRectangle.Width/10, ClientRectangle.Height/4));
            base.OnPaint(e);
        }
        protected override void OnMouseDoubleClick(MouseEventArgs e)
        {
            base.OnMouseDoubleClick(e);
            LoadImage();
        }
        protected override void OnResize(EventArgs e)
        {
            PaintStruct ps;
            ps.dc = null;
            ps.isPaint = false;
            DrawImage(ps);
            base.OnResize(e);
        }
        
        protected override void OnMouseDown(MouseEventArgs e)
        {
            Invalidate();
            nDblClick = 0;
            base.OnMouseDown(e);
        }
        protected override void OnMouseUp(MouseEventArgs e)
        {
            if (rcNew.Width > 0 && rcNew.Height > 0);
            else
            {
                base.OnMouseUp(e);
                return;
            }
            if (nDblClick == -1)
            {
                base.OnMouseUp(e);
                return;
            }
            ChildFrm chFrm = new ChildFrm();
            //Graphics dc = CreateGraphics();

            chFrm.img = new Bitmap(rcNew.Width, rcNew.Height);
            //Image img = new Bitmap(rcNew.Width, rcNew.Height);
            Graphics dcMem = Graphics.FromImage(chFrm.img);
            //Graphics dcMem = Graphics.FromImage(img);
            dcMem.Clear(Color.Azure);
            dcMem.DrawImage(imgResize, new Rectangle(0, 0, rcNew.Width, rcNew.Height), rcNew, GraphicsUnit.Pixel);
            dcMem.Dispose();
            
            chFrm.Size = new Size(chFrm.img.Width 4,chFrm.img.Height 34);
            chFrm.Show();
            //dc.DrawImage(img, new Point(0, 0));
            nDblClick = -1;
            base.OnMouseUp(e);
        }
        
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Escape)
            {
                base.Close();               
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        public void BlockText(Graphics dc, int a, int b)
        {
            //Font font = new Font("verdana", ClientRectangle.Width * 37 / 452, FontStyle.Bold);
            Font font = new Font("verdana", 12, FontStyle.Bold);
            int x = a;
            int y = b;
            string str = "With Left mouse button down\ndrag the mouse";
            int j = 0;
            dc.SmoothingMode = SmoothingMode.HighQuality;
            dc.PixelOffsetMode = PixelOffsetMode.HighQuality;
            for (j = 0; j < 7; j  = 1)
            {
                dc.DrawString(str, font, Brushes.Black, new PointF(x   j, y   j));
            }
            SizeF sizef = dc.MeasureString(str, Font);
            sizef.Width /= 8;
            sizef.Height /= 8;
            PointF ptf = new PointF(x   j, y   j);
            RectangleF rectf = new RectangleF(ptf, sizef);
            LinearGradientBrush lgBrush = new LinearGradientBrush(rectf, Color.Yellow, Color.Red, LinearGradientMode.ForwardDiagonal);
            lgBrush.WrapMode = WrapMode.TileFlipXY;
            dc.DrawString(str, font, lgBrush, ptf);
        }
        public void ReflectedBLockText(Graphics dc, Point pt)
        {
            Point p = pt;
            p.Offset(0, ClientRectangle.Height * 100 / 351);
            BlockText(dc, p.X, p.Y);
            string str = "Resize or Double click";
            //Font font = new Font("Tahoma", ClientRectangle.Width * 27 / 452, FontStyle.Bold);
            Font font = new Font("Tahoma", 15, FontStyle.Bold);
            SizeF sz = dc.MeasureString(str, font);

            StringFormat strFmt = StringFormat.GenericDefault;
            Rectangle rcTmp = new Rectangle(pt.X, pt.Y, (int)(sz.Width   1), (int)(sz.Height   1));

            LinearGradientBrush brLinear = new LinearGradientBrush(new Point(0, rcTmp.Height / 4), new Point(0, rcTmp.Height / 8), Color.Red, Color.White);
            brLinear.WrapMode = WrapMode.TileFlipX;

            dc.TranslateTransform(pt.X, pt.Y);
            GraphicsState dcState = dc.Save();

            int j = 0;
            for (j = 0; j < 5; j  )
            {
                dc.DrawString(str, font, Brushes.Yellow, new PointF(-j, j));
            }

            dc.DrawString(str, font, brLinear, -j, j, strFmt);
            dc.ScaleTransform(1, -1);
            dc.DrawString(str, font, brLinear, -j, (float)-(1.59 * sz.Height), strFmt);
            dc.Restore(dcState);
            //dc.Dispose();
        }
    }
}