Winform+.Net6實現(xiàn)圖片拖拽上傳功能
前言
如題,跟你使用過的某些拖拽上傳的網(wǎng)站或者Web框架一樣,將圖片拖拽到指定位置后直接進行上傳以及預覽,減少找文件、操作的時間。本文主要使用WinformPictureBox
控件+.Net6 WebApi實現(xiàn)。
開發(fā)環(huán)境:.NET Framework4.8+.Net6
開發(fā)工具:Visual Studio 2022
實現(xiàn)步驟
1.創(chuàng)建自定義控件,繼承自PictureBox
,然后定義以下屬性
private string _NullDesc; [Description("沒有圖像時的描述")] public string NullDesc { get { return _NullDesc; } set { _NullDesc = value; Invalidate(); } } private Font _NullDescFont; [Description("沒有圖像時的描述字體")] public Font NullDescFont { get { return _NullDescFont; } set { _NullDescFont = value; Invalidate(); } } private Color _NullDescFontColor; [Description("沒有圖像時的描述字體顏色")] 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.后臺接收文件
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 "上傳成功"; }
實現(xiàn)效果
到此這篇關(guān)于Winform+.Net6實現(xiàn)圖片拖拽上傳功能的文章就介紹到這了,更多相關(guān)Winform .Net圖片上傳內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#中Array與ArrayList用法及轉(zhuǎn)換的方法
C#中Array與ArrayList用法及轉(zhuǎn)換的方法,需要的朋友可以參考一下2013-03-03C#中實現(xiàn)判斷某個類是否實現(xiàn)了某個接口
這篇文章主要介紹了C#中實現(xiàn)判斷某個類是否實現(xiàn)了某個接口,本文給出了多種判斷方法,需要的朋友可以參考下2015-06-06C#調(diào)用存儲過程詳解(帶返回值、參數(shù)輸入輸出等)
這篇文章主要介紹了C#調(diào)用存儲過程的方法,結(jié)合實例形式詳細分析了各種常用的存儲過程調(diào)用方法,包括帶返回值、參數(shù)輸入輸出等,需要的朋友可以參考下2016-06-06