在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#图形和图像处理 → 多图像平滑无闪烁移动基础代码

多图像平滑无闪烁移动基础代码

C#图形和图像处理

下载此实例
  • 开发语言:C#
  • 实例大小:0.60M
  • 下载次数:39
  • 浏览次数:361
  • 发布时间:2019-06-09
  • 实例类别:C#图形和图像处理
  • 发 布 人:无聊啊~~
  • 文件格式:.rar
  • 所需积分:1
 相关标签: 基础 图像 移动 闪烁 代码

实例介绍

【实例简介】pictiuebox效率低,这里使用高效重绘方法,vs2017,代码很短,鼠标判断等就200行,只使用了panel 实现。

可添加多个图像元素,移动元素并调整元素图层。

【实例截图】

from clipboard

【核心代码】

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace Demo_1
{
    public partial class Form1 : Form
    {
        public class PanelEx : Panel
        {
            public PanelEx()
            {
                SetStyle(
                    ControlStyles.AllPaintingInWmPaint |
                    ControlStyles.OptimizedDoubleBuffer |
                    ControlStyles.ResizeRedraw |
                    ControlStyles.SupportsTransparentBackColor |
                    ControlStyles.UserPaint,
                    true
                    );
                UpdateStyles();
            }
        }

        public abstract class DraggableObject
        {
            public abstract string Id { get; set; }
            public abstract int prim_X { get; set; }
            public abstract int prim_Y { get; set; }
            public abstract Rectangle Region { get; set; }
            public abstract Bitmap Setimage { get; set; }
            public abstract void OnPaint(PaintEventArgs e);
        }

        public class Draggable : DraggableObject
        {
            private string m_Id;
            private int m_primX;
            private int m_primY;
            private Rectangle m_Region;
            private Bitmap m_SetImage;

            public Draggable(string id, Rectangle regin, Bitmap image)
            {
                m_Id = id;
                m_Region = regin;
                m_SetImage = image;
            }
            public override string Id { get => m_Id; set => m_Id = value; }
            public override int prim_X { get => m_primX; set => m_primX = value; }
            public override int prim_Y { get => m_primY; set => m_primY = value; }
            public override Rectangle Region { get => m_Region; set => m_Region = value; }
            public override Bitmap Setimage { get => m_SetImage; set => m_SetImage = value; }
            public override void OnPaint(PaintEventArgs e)
            {
                e.Graphics.DrawImage(m_SetImage, m_Region);
            }
        }
        public List<DraggableObject> DesignObjects = new List<DraggableObject>();

        public PanelEx panelContent;
        public Label info;
        private bool Is_Dragging = false;
        private int index = -1,record = -1;

        public Form1()
        {
            InitializeComponent();

            panelContent = new PanelEx()
            {
                Dock = DockStyle.Fill,
                BackgroundImageLayout = ImageLayout.Stretch,
                BackgroundImage = Image.FromFile("back.jpg")
            };
            panelContent.Paint  = PanelContent_Paint;
            panelContent.MouseMove  = PanelContent_MouseMove;
            panelContent.MouseDown  = PanelContent_MouseDown;
            panelContent.MouseUp  = PanelContent_MouseUp;
            this.Controls.Add(panelContent);

            PanelEx panelTop = new PanelEx()
            {
                Height = 30,
                Dock = DockStyle.Top
            };
            this.Controls.Add(panelTop);

            Button[] buttons = new Button[4];
            string[] btnText = new string[] { "添加元素","删除元素","元素前移","元素后移"};
            for (int i=0;i<4;i  )
            {
                buttons[i] = new Button()
                {
                    Size = new Size(70, 25),
                    Location = new Point(10 i*80,5),
                    Font = new Font("微软雅黑",9,FontStyle.Regular),
                    Text = btnText[i],
                    Tag = i
                };
                buttons[i].Click  =Button_Click;
                panelTop.Controls.Add(buttons[i]);
            }

            info = new Label()
            {
                AutoSize = true,
                Location = new Point(350,5),
                Font = new Font("微软雅黑", 9, FontStyle.Regular),
            };
            panelTop.Controls.Add(info);
        }

        private void PanelContent_Paint(object sender, PaintEventArgs e)
        {
            foreach (DraggableObject item in DesignObjects)
            {
                item.OnPaint(e);
            }
        }

        private void Button_Click(object sender, EventArgs e)
        {
            Control pL = (Control)sender;

            if ((int)pL.Tag == 0)
            {
                OpenFileDialog openImgDialog = new OpenFileDialog();
                openImgDialog.Filter = "图像文件(*.png;*.jpg;*.gif;*.bmp)|*.png;*.jpg;*.gif;*.bmp";
                openImgDialog.RestoreDirectory = true;
                openImgDialog.FilterIndex = 1;
                if (openImgDialog.ShowDialog() == DialogResult.OK)
                {
                    Bitmap bmp = Image.FromFile(openImgDialog.FileName) as Bitmap;
                    Draggable draggableBlock = new Draggable(DateTime.Now.ToString(), new Rectangle(50, 50, bmp.Width, bmp.Height), bmp);
                    DesignObjects.Add(draggableBlock);
                    record = DesignObjects.Count - 1;
                    info.Text = "当前元素:"   (record   1)   "  ID: "   DesignObjects[record].Id;
                }
            }
            else
            if ((int)pL.Tag == 1)
            {
                if (record != -1)
                {
                    DesignObjects.RemoveAt(record);
                    record = DesignObjects.Count == 0 ? -1 : DesignObjects.Count - 1;
                }
            }
            else
            if ((int)pL.Tag == 2)
            {
                if (record != -1)
                {
                    var swap = DesignObjects[record];
                    DesignObjects.RemoveAt(record);
                    DesignObjects.Insert(0, swap);
                    record = 0;
                }
            }
            if ((int)pL.Tag == 3)
            {
                if (record != -1)
                {
                    var swap = DesignObjects[record];
                    DesignObjects.RemoveAt(record);
                    DesignObjects.Add(swap);
                    record = DesignObjects.Count - 1;
                }
            }
            if (record != -1) info.Text = "当前元素:"   (record   1)   "  ID: "  DesignObjects[record].Id;
            panelContent.Invalidate();
        }

        private void PanelContent_MouseUp(object sender, MouseEventArgs e)
        {
            Is_Dragging = false;
        }

        private void PanelContent_MouseDown(object sender, MouseEventArgs e)
        {
            index = DesignObjects.FindIndex(d => d.Region.Contains(e.Location));
            if (index != -1)
            {
                record = index;
                Is_Dragging = true;
                DesignObjects[index].prim_X = e.Location.X - DesignObjects[index].Region.Left;
                DesignObjects[index].prim_Y = e.Location.Y - DesignObjects[index].Region.Top;
                info.Text = "当前元素:"   (record   1)   "  ID: "   DesignObjects[record].Id;
            }
            else
            {
                record = -1;
                Is_Dragging = false;
            }
        }

        private void PanelContent_MouseMove(object sender, MouseEventArgs e)
        {
            Control pL = (Control)sender;

            if (Is_Dragging && index != -1)
            {
                int set_x = e.Location.X - DesignObjects[index].prim_X;
                int set_y = e.Location.Y - DesignObjects[index].prim_Y;
                set_x = set_x < 0 ? 0 : set_x = set_x > panelContent.Width - DesignObjects[index].Region.Width ? panelContent.Width - DesignObjects[index].Region.Width : set_x;
                set_y = set_y < 0 ? 0 : set_y = set_y > panelContent.Height - DesignObjects[index].Region.Height ? panelContent.Height - DesignObjects[index].Region.Height : set_y;
                DesignObjects[index].Region = new Rectangle(set_x, set_y, DesignObjects[index].Region.Width, DesignObjects[index].Region.Height);
                panelContent.Invalidate();
            }
            else
            {
                index = DesignObjects.FindIndex(d => d.Region.Contains(e.Location));
                if (index != -1) pL.Cursor = Cursors.SizeAll;
                else pL.Cursor = Cursors.Default;
            }
        }
    }
}



实例下载地址

多图像平滑无闪烁移动基础代码

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

第 1 楼 zhshyi 发表于: 2020-02-07 15:27 50
public override string Id { get => m_Id; set => m_Id = value; } public override int prim_X { get => m_primX; set => m_primX = value; } public override int prim_Y { get => m_primY; set => m_primY = value; } public override Rectangle Region { get => m_Region; set => m_Region = value; } public override Bitmap Setimage { get => m_SetImage; set => m_SetImage = value; }这段报错

支持(0) 盖楼(回复)

第 2 楼 我想你了、 发表于: 2020-02-09 09:27 24
楼上的,C#7.0语法,换高版本VS

支持(0) 盖楼(回复)

第 3 楼 Barrett2 发表于: 2020-04-28 17:13 58
哪个是图片移动的主要代码呀

支持(0) 盖楼(回复)

第 4 楼 Barrett2 发表于: 2020-04-28 17:14 01
哪个是图片移动的主要代码呀

支持(0) 盖楼(回复)

发表评论

(您的评论需要经过审核才能显示)

查看所有4条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警