zoukankan      html  css  js  c++  java
  • PictureBox从本地上传图片和保存在磁盘目录

    private void mypicbox_Click(object sender, EventArgs e)
    {
    try
    {
    OpenFileDialog ofdPic = new OpenFileDialog();
    ofdPic.Filter = "*.jpg,*.jpeg,*.bmp,*.gif,*.ico,*.png,*.tif,*.wmf|*.jpg;*.jpeg;*.bmp;*.gif;*.ico;*.png;*.tif;*.wmf";
    ofdPic.FilterIndex = 1;
    ofdPic.FileName = "";
    if (ofdPic.ShowDialog() == DialogResult.OK)
    {
    string sPicPaht = ofdPic.FileName.ToString();
    Bitmap bmPic = new Bitmap(sPicPaht);
    Point ptLoction = new Point(bmPic.Size);
    if (ptLoction.X > mypicbox.Size.Width || ptLoction.Y > mypicbox.Size.Height)
    {
    //图像充滿图像框,並且图像維持比例
    mypicbox.SizeMode = PictureBoxSizeMode.Zoom;
    }
    else
    {
    //图像在图像框置中
    mypicbox.SizeMode = PictureBoxSizeMode.CenterImage;
    }
    //获取图像的Path
    mypicbox.Load(sPicPaht);
    mypicbox.Image = this.mypicbox.Image;
    AutoSave(mypicbox);
    }
    }
    catch (Exception exp)
    {
    throw exp;
    }
    }
    private void AutoSave(PictureBox mypicbox)
    {
    string Opath = @"DataImage";
    string photoname = DateTime.Now.Ticks.ToString();
    this.imageName = photoname;
    if (Opath.Substring(Opath.Length - 1, 1) != @"")
    Opath = Opath + @"";
    string path1 = Opath + DateTime.Now.ToShortDateString();
    if (!Directory.Exists(path1))
    {
    Directory.CreateDirectory(path1);
    }
    //pictureBox1.Image.Save(path1 +"\" + photoname + ".jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
    //图像的缩小
    System.Drawing.Bitmap objPic, objNewPic;
    try
    {
    //mypicbox.Image.Save(path1);
    objPic = new System.Drawing.Bitmap(mypicbox.Image);
    objNewPic = new System.Drawing.Bitmap(objPic, mypicbox.Width, mypicbox.Height);
    //objNewPic=new System.Drawing.Bitmap(objPic,320,240);//图片保存的大小尺寸
    objNewPic.Save(path1 + "\" + imageName + ".jpg");
    }
    catch (Exception exp) { throw exp; }
    finally
    {
    objPic = null;
    objNewPic = null;
    }
    }
    private string imageName { get; set; }

  • 相关阅读:
    【转载】python字符格式化
    【python】生产者消费者问题优化
    【python】pickle文件读写的insecure问题
    【工具使用】sublime设置换行符为unix风格
    【python】CGI部署问题解决
    【python】引用c的dll
    【多线程】Python进程,队列和锁相关的一些问题
    【Python】下载图片,标识之后转发出去
    【Python】PIL在window64位机制上引用异常问题解决
    【Python网页分析】httplib库的重定向处理
  • 原文地址:https://www.cnblogs.com/zhangruisoldier/p/4272799.html
Copyright © 2011-2022 走看看