zoukankan      html  css  js  c++  java
  • winform程序中打开和保存一幅图像

          作为一个c#的初学者,开始感觉自己对于这方面的知识很茫然,懂得非常少,但是我没有放弃,所以我相信坚持下去会好的,对于每一位初学者,大家都不要放弃,大家彼此互勉,下面和大家分享下我写的一个简单的小程序:

          打开和保存一幅图像。本人用的vs2010中的winform利用button控件、pictureBox控件;

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using System.Drawing.Imaging;

    namespace 对话框的使用
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
    OpenFileDialog ofd = new OpenFileDialog();
    ofd.Multiselect = true;
    ofd.Title = "打开一幅图像";
    ofd.Filter = "媒体文件(*.mp4)|*.mp4|图片文件(*.jpg)|*.jpg|文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
    ofd.ShowDialog();
    string path = ofd.FileName;
    pictureBox1.Image = Image.FromFile(path);
    pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
    }

    private void button2_Click(object sender, EventArgs e)
    {
    SaveFileDialog sfd = new SaveFileDialog();
    sfd.Title = "保存图片";
    sfd.Filter = "图片文件(*bmp)|*.bmp";
    sfd.FilterIndex = 0;
    sfd.ShowDialog();
    string path = sfd.FileName;
    using (FileStream fsWrite = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
    {
    if (pictureBox1.Image != null)
    {
    pictureBox1.Image.Save(fsWrite, System.Drawing.Imaging.ImageFormat.Bmp);
    }

    }
    }
    }
    }

  • 相关阅读:
    Android Studio代码自己主动检測错误提示
    uva 1567
    UWP 新手教程2——怎样实现自适应用户界面
    远程服务的使用场景
    本地服务和远程服务
    本地应用调用远程服务中的方法
    混合方式开启服务
    绑定服务抽取接口
    绑定服务调用服务里的方法
    bind绑定服务的生命周期
  • 原文地址:https://www.cnblogs.com/smart--boy/p/5993186.html
Copyright © 2011-2022 走看看