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

Winform+.Net6實(shí)現(xiàn)圖片拖拽上傳功能

 更新時(shí)間:2023年09月11日 11:35:26   作者:Csharp小記  
這篇文章主要為大家詳細(xì)介紹了如何使用WinformPictureBox控件+.Net6 WebApi實(shí)現(xiàn)圖片拖拽上傳功能,文中的示例代碼講解詳細(xì),感興趣的可以學(xué)習(xí)一下

前言

如題,跟你使用過(guò)的某些拖拽上傳的網(wǎng)站或者Web框架一樣,將圖片拖拽到指定位置后直接進(jìn)行上傳以及預(yù)覽,減少找文件、操作的時(shí)間。本文主要使用WinformPictureBox控件+.Net6 WebApi實(shí)現(xiàn)。

開發(fā)環(huán)境:.NET Framework4.8+.Net6

開發(fā)工具:Visual Studio 2022

實(shí)現(xiàn)步驟

1.創(chuàng)建自定義控件,繼承自PictureBox,然后定義以下屬性

private string _NullDesc;
        [Description("沒(méi)有圖像時(shí)的描述")]
        public string NullDesc { get { return _NullDesc; } set { _NullDesc = value; Invalidate(); } }
        private Font _NullDescFont;
        [Description("沒(méi)有圖像時(shí)的描述字體")]
        public Font NullDescFont { get { return _NullDescFont; } set { _NullDescFont = value; Invalidate(); } }
        private Color _NullDescFontColor;
        [Description("沒(méi)有圖像時(shí)的描述字體顏色")]
        public Color NullDescFontColor { get { return _NullDescFontColor; } set { _NullDescFontColor = value; Invalidate(); } }
        [Description("上傳事件")]
        public event EventHandler Upload;
        public new Image Image
        {
            get
            {
                return base.Image;
            }
            set
            {
                base.Image = value;
                if (value != null)
                {
                    Upload?.Invoke(this, new EventArgs());
                }
            }
        }

2.處理拖拽事件

protected override void OnDragEnter(DragEventArgs drgevent)
        {
            base.OnDragEnter(drgevent);
            if (drgevent.Data.GetDataPresent(DataFormats.FileDrop) || drgevent.Data.GetDataPresent(DataFormats.Bitmap))
            {
                drgevent.Effect = DragDropEffects.Copy;
            }
            else
            {
                drgevent.Effect = DragDropEffects.None;
            }
        }
        protected override void OnDragDrop(DragEventArgs drgevent)
        {
            base.OnDragDrop(drgevent);
            Image = GetImage(drgevent.Data);
        }

3.重寫OnPaint事件,做以下處理

protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            if (Image == null && BackgroundImage == null && !string.IsNullOrWhiteSpace(NullDesc))
            {
                SizeF sf = g.MeasureString(NullDesc, NullDescFont, Size);
                float x = (Width - sf.Width) / 2;
                float y = (Height - sf.Height) / 2;
                g.DrawString(NullDesc, Font, new SolidBrush(NullDescFontColor), new PointF(x, y));
            }
        }

4.在Upload事件中完成上傳

  private async void PictureBoxEx1_Upload(object sender, EventArgs e)
        {
            HttpClient client = new HttpClient();
            MemoryStream memoryStream = new MemoryStream();
            Image img = (Image)pictureBoxEx1.Image.Clone();
            img.Save(memoryStream, img.RawFormat);
            memoryStream.Seek(0, SeekOrigin.Begin);
            MultipartFormDataContent content = new MultipartFormDataContent();
            StreamContent streamContent = new StreamContent(memoryStream);
            streamContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data")
            {
                Name = "file",
                FileName = "upload" + GetExtension(img)
            };
            content.Add(streamContent);
            var result =await client.PostAsync("https://localhost:7075/Default/Upload", content);
            MessageBox.Show(await result.Content.ReadAsStringAsync());
        }

5.后臺(tái)接收文件

 public string Upload(IFormFile file)
        {
            string basePath = AppContext.BaseDirectory + "upload\\";
            if (file == null || file.Length == 0)
            {
                return "文件不可為空";
            }
            if (!Directory.Exists(basePath))
            {
                Directory.CreateDirectory(basePath);
            }
            string filter = Path.GetExtension(file.FileName);
            string fileName = DateTime.Now.Ticks + filter;
            string savePath = basePath + fileName;
            using var stream = new FileStream(savePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            file.CopyTo(stream);
            return "上傳成功";
        }

實(shí)現(xiàn)效果

到此這篇關(guān)于Winform+.Net6實(shí)現(xiàn)圖片拖拽上傳功能的文章就介紹到這了,更多相關(guān)Winform .Net圖片上傳內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論