C# winfrom写了一个美观的登陆窗体 login

记录一下,同时本着互联网的分享精神分享出来,以便后来者引用。先放效果图:

这里用到几个东西,gif好看的背景图好难找,基本都是需要钱下载,我这里放两个给需要用到的小伙伴,告诉一下大家:这些好看的动图我都是去那些需要钱下载的网站,然后点击进去后用gif录制工具给录制的,推荐这个工具给大家:GifCam,直接搜索去官网下载这个软件就好。

我这个登陆窗体用到两个功能点:1、是用到自定义圆角窗体的代码段,首先先把属性:backgroundimagelayout=Stretch;接着fromborderstyle=none;2、运用GDI+来加载动态图片GIF的到背景图,直接在属性里设置不行,这里有一个注意的地方就是如果你运行起来发现背景图有闪烁,要去设置窗体属性DoubleBuffered为true。

 

这里贴一下源码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using Oracle.ManagedDataAccess.Client;
using System.Configuration;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
using SpeechLib;
using System.Drawing.Imaging;
namespace web_quece
{
    public partial class login_queue : Form
    {
        DB_Contr bc = new DB_Contr();
        DataView dv = new DataView();
        SetWD swd = new SetWD();

        public login_queue()
        {
            InitializeComponent();
        }
        //窗体移动的代码引用

        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int IParam);
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;


        private void Main_MouseDown(object sender, MouseEventArgs e)
        {
            //拖动窗体 
            this.Cursor = System.Windows.Forms.Cursors.Hand;//改变鼠标样式 
            ReleaseCapture();
            SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
            this.Cursor = System.Windows.Forms.Cursors.Default;
        }

        //窗体resize事件窗体调用窗体圆角设置函数
        private void Form1_Resize(object sender, EventArgs e)
        {
            SetWindowRegion();
        }
        public void SetWindowRegion()
        {//设置窗体圆角函数,同时条用类setWD的函数。

            System.Drawing.Drawing2D.GraphicsPath FormPath;
            FormPath = new System.Drawing.Drawing2D.GraphicsPath();
            Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
            FormPath = swd.GetRoundedRectPath(rect, 10);
            this.Region = new Region(FormPath);

        }
        private GraphicsPath GetRoundedRectPath(Rectangle rect, int radius)
        {
            int diameter = radius;
            Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter));
            GraphicsPath path = new GraphicsPath();

            // 左上角
            path.AddArc(arcRect, 180, 90);

            // 右上角
            arcRect.X = rect.Right - diameter;
            path.AddArc(arcRect, 270, 90);

            // 右下角
            arcRect.Y = rect.Bottom - diameter;
            path.AddArc(arcRect, 0, 90);

            // 左下角
            arcRect.X = rect.Left;
            path.AddArc(arcRect, 90, 90);
            path.CloseFigure();//闭合曲线
            return path;
        }

        //给窗体加载gif背景图
       Bitmap bitmap = new Bitmap(Application.StartupPath + "\\GIF_01.gif");                //实例化一个GDI 绘图图面对象
        bool current = false;           //初始化一个bool型的变量current
        public void PlayImage()
        {
            if (!current)           //当该值为true时
            {
                ImageAnimator.Animate(bitmap, new EventHandler(this.OnFrameChanged));   //将多帧图片显示为动画图像
                current = true;         //设定current的值为true
            }
        }
        private void OnFrameChanged(object o, EventArgs e)
        {
            this.Invalidate();          //使控件的整个图片无效并导致重绘事件
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.DrawImage(this.bitmap, new Point(1, 1));     //从指定的位置绘制图片
            ImageAnimator.UpdateFrames();                   //使该帧在当前正被图画处理的所有图像中前移
        }


        private void login_queue_Load(object sender, EventArgs e)
        {
            PlayImage();            //播放
            ImageAnimator.Animate(bitmap, new EventHandler(this.OnFrameChanged));   //将多帧图像显示为动画
        }

    }
}
  
//此处为引用的定义窗体类,之前忘记贴出来了,2021年8月23日更新
 class Set_WD
    {
        //定义圆角窗体画笔类
        public GraphicsPath GetRoundedRectPath(Rectangle rect, int radius)
        {
            int diameter = radius;
            Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter));
            GraphicsPath path = new GraphicsPath();

            // 左上角
            path.AddArc(arcRect, 180, 90);

            // 右上角
            arcRect.X = rect.Right - diameter;
            path.AddArc(arcRect, 270, 90);

            // 右下角
            arcRect.Y = rect.Bottom - diameter;
            path.AddArc(arcRect, 0, 90);

            // 左下角
            arcRect.X = rect.Left;
            path.AddArc(arcRect, 90, 90);
            path.CloseFigure();//闭合曲线
            return path;
        }
    }

C# WinForms 应用程序中,将图片上传并存储到数据库通常涉及将图片转换为字节数组(`byte[]`),然后将其保存到数据库中的 `VARBINARY` 类型字段中。以下是一个完整的示例,展示如何实现图片上传并存储到 SQL Server 数据库。 ### 数据库设计 假设有一个名为 `Images` 的表,结构如下: ```sql CREATE TABLE Images ( ImageID INT PRIMARY KEY IDENTITY(1,1), ImageName NVARCHAR(100), ImageData VARBINARY(MAX) ); ``` ### C# WinForms 实现代码 ```csharp using System; using System.Data; using System.Data.SqlClient; using System.IO; using System.Windows.Forms; namespace ImageStorageApp { public partial class MainForm : Form { private string connectionString = "你的数据库连接字符串"; public MainForm() { InitializeComponent(); } // 打开文件对话框选择图片 private void btnSelectImage_Click(object sender, EventArgs e) { using (OpenFileDialog openFileDialog = new OpenFileDialog()) { openFileDialog.Filter = "Image Files|*.jpg;*.jpeg;*.png;*.bmp"; if (openFileDialog.ShowDialog() == DialogResult.OK) { string filePath = openFileDialog.FileName; pictureBox.Image = System.Drawing.Image.FromFile(filePath); txtImageName.Text = Path.GetFileName(filePath); } } } // 将图片存储到数据库 private void btnSaveToDatabase_Click(object sender, EventArgs e) { if (pictureBox.Image == null || string.IsNullOrEmpty(txtImageName.Text)) { MessageBox.Show("请先选择一张图片并输入图片名称。"); return; } byte[] imageData = ImageToByteArray(pictureBox.Image); using (SqlConnection connection = new SqlConnection(connectionString)) { string query = "INSERT INTO Images (ImageName, ImageData) VALUES (@ImageName, @ImageData)"; using (SqlCommand command = new SqlCommand(query, connection)) { command.Parameters.AddWithValue("@ImageName", txtImageName.Text); command.Parameters.AddWithValue("@ImageData", imageData); connection.Open(); command.ExecuteNonQuery(); MessageBox.Show("图片已成功保存到数据库!"); } } } // 将图片转换为字节数组 private byte[] ImageToByteArray(System.Drawing.Image image) { using (MemoryStream ms = new MemoryStream()) { image.Save(ms, System.Drawing.Imaging.ImageFormat.Png); return ms.ToArray(); } } } } ``` ### 说明 - `OpenFileDialog` 用于选择图片文件。 - `ImageToByteArray` 方法将 `Image` 对象转换为 `byte[]`,以便可以存储在数据库中。 - 使用 `SqlConnection` 和 `SqlCommand` 将图片数据插入到数据库表中。 - `PictureBox` 用于预览选择的图片。 ### 数据库查询示例 从数据库中检索图片的示例代码如下: ```csharp private void LoadImageFromDatabase(int imageId) { using (SqlConnection connection = new SqlConnection(connectionString)) { string query = "SELECT ImageName, ImageData FROM Images WHERE ImageID = @ImageID"; using (SqlCommand command = new SqlCommand(query, connection)) { command.Parameters.AddWithValue("@ImageID", imageId); connection.Open(); using (SqlDataReader reader = command.ExecuteReader()) { if (reader.Read()) { byte[] imageData = (byte[])reader["ImageData"]; using (MemoryStream ms = new MemoryStream(imageData)) { pictureBox.Image = System.Drawing.Image.FromStream(ms); } } } } } } ``` 通过上述代码,可以在 C# WinForms 应用程序中实现图片上传、存储和检索功能。
评论 7
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值