Winform+.Net6实现图片拖拽上传功能
作者:Csharp小记
这篇文章主要为大家详细介绍了如何使用WinformPictureBox控件+.Net6 WebApi实现图片拖拽上传功能,文中的示例代码讲解详细,感兴趣的可以学习一下
前言
如题,跟你使用过的某些拖拽上传的网站或者Web框架一样,将图片拖拽到指定位置后直接进行上传以及预览,减少找文件、操作的时间。本文主要使用WinformPictureBox
控件+.Net6 WebApi实现。
开发环境:.NET Framework4.8+.Net6
开发工具:Visual Studio 2022
实现步骤
1.创建自定义控件,继承自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 "上传成功"; }
实现效果
到此这篇关于Winform+.Net6实现图片拖拽上传功能的文章就介绍到这了,更多相关Winform .Net图片上传内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!