当前位置:   article > 正文

Winform+.Net6实现图片拖拽上传

Winform+.Net6实现图片拖拽上传

845a034c0758c64039c530dbf2c6699a.png

前言

da48e68040a6872340926a42b2a2b0aa.png

269ea8cf6400e71894deab97cd4016b5.png

    如题,跟你使用过的某些拖拽上传的网站或者Web框架一样,将图片拖拽到指定位置后直接进行上传以及预览,减少找文件、操作的时间。本文主要使用WinformPictureBox控件+.Net6 WebApi实现。

5ca913a3d98aa900efacd5f19cd0cb15.png

3d3c7efcb9dc173f5274336cca62014f.gif

d14c61e0d76b40f55c3e252976c1c231.jpeg

开发环境:.NET Framework4.8+.Net6

开发工具:Visual Studio 2022

ff3b1b994d4dfada18c5a91e5dc5ce69.png

实现步骤

c2619c457b97e5261bd12e32189e291a.png

  1. 创建自定义控件,继承自PictureBox,然后定义以下属性

    1. private string _NullDesc;
    2. [Description("没有图像时的描述")]
    3. public string NullDesc { get { return _NullDesc; } set { _NullDesc = value; Invalidate(); } }
    4. private Font _NullDescFont;
    5. [Description("没有图像时的描述字体")]
    6. public Font NullDescFont { get { return _NullDescFont; } set { _NullDescFont = value; Invalidate(); } }
    7. private Color _NullDescFontColor;
    8. [Description("没有图像时的描述字体颜色")]
    9. public Color NullDescFontColor { get { return _NullDescFontColor; } set { _NullDescFontColor = value; Invalidate(); } }
    10. [Description("上传事件")]
    11. public event EventHandler Upload;
    12. public new Image Image
    13. {
    14. get
    15. {
    16. return base.Image;
    17. }
    18. set
    19. {
    20. base.Image = value;
    21. if (value != null)
    22. {
    23. Upload?.Invoke(this, new EventArgs());
    24. }
    25. }
    26. }
  2. 处理拖拽事件

  1. protected override void OnDragEnter(DragEventArgs drgevent)
  2. {
  3. base.OnDragEnter(drgevent);
  4. if (drgevent.Data.GetDataPresent(DataFormats.FileDrop) || drgevent.Data.GetDataPresent(DataFormats.Bitmap))
  5. {
  6. drgevent.Effect = DragDropEffects.Copy;
  7. }
  8. else
  9. {
  10. drgevent.Effect = DragDropEffects.None;
  11. }
  12. }
  13. protected override void OnDragDrop(DragEventArgs drgevent)
  14. {
  15. base.OnDragDrop(drgevent);
  16. Image = GetImage(drgevent.Data);
  17. }
  1. 重写OnPaint事件,做以下处理

    1. protected override void OnPaint(PaintEventArgs e)
    2. {
    3. base.OnPaint(e);
    4. Graphics g = e.Graphics;
    5. if (Image == null && BackgroundImage == null && !string.IsNullOrWhiteSpace(NullDesc))
    6. {
    7. SizeF sf = g.MeasureString(NullDesc, NullDescFont, Size);
    8. float x = (Width - sf.Width) / 2;
    9. float y = (Height - sf.Height) / 2;
    10. g.DrawString(NullDesc, Font, new SolidBrush(NullDescFontColor), new PointF(x, y));
    11. }
    12. }
  1. Upload事件中完成上传

  1. private async void PictureBoxEx1_Upload(object sender, EventArgs e)
  2. {
  3. HttpClient client = new HttpClient();
  4. MemoryStream memoryStream = new MemoryStream();
  5. Image img = (Image)pictureBoxEx1.Image.Clone();
  6. img.Save(memoryStream, img.RawFormat);
  7. memoryStream.Seek(0, SeekOrigin.Begin);
  8. MultipartFormDataContent content = new MultipartFormDataContent();
  9. StreamContent streamContent = new StreamContent(memoryStream);
  10. streamContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data")
  11. {
  12. Name = "file",
  13. FileName = "upload" + GetExtension(img)
  14. };
  15. content.Add(streamContent);
  16. var result =await client.PostAsync("https://localhost:7075/Default/Upload", content);
  17. MessageBox.Show(await result.Content.ReadAsStringAsync());
  18. }
  1. 后台接收文件

  1. public string Upload(IFormFile file)
  2. {
  3. string basePath = AppContext.BaseDirectory + "upload\\";
  4. if (file == null || file.Length == 0)
  5. {
  6. return "文件不可为空";
  7. }
  8. if (!Directory.Exists(basePath))
  9. {
  10. Directory.CreateDirectory(basePath);
  11. }
  12. string filter = Path.GetExtension(file.FileName);
  13. string fileName = DateTime.Now.Ticks + filter;
  14. string savePath = basePath + fileName;
  15. using var stream = new FileStream(savePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
  16. file.CopyTo(stream);
  17. return "上传成功";
  18. }

4dd43608b3e9901e9e9b5e3475af3ef2.png

实现效果

abcd72914c1a432bb678786926759d6f.png

9eac4cff4284cd7c73d52c6939babba0.gif

  1. -技术群:添加小编微信并备注进群
  2. 小编微信:mm1552923   公众号:dotNet编程大全
本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
  

闽ICP备14008679号