首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

Bit地图保存图片全是黑的

2013-10-12 
Bitmap保存图片全是黑的using Systemusing System.Collections.Genericusing System.ComponentModelusi

Bitmap保存图片全是黑的


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

namespace frmSxqm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private Graphics g;
        private int sX, sY; 
        private Bitmap bitmap; 

        private void Form1_Load(object sender, EventArgs e)
        {
            bitmap = new Bitmap(300, 300);
            g = Graphics.FromImage(bitmap); 
            Cursor.Current = Cursors.Default;
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            sX = e.X;
            sY = e.Y; 
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            g.DrawLine(new Pen(Color.Black, 3), sX, sY, e.X, e.Y); 
            sX = e.X; sY = e.Y; 
            this.pictureBox1.Image = bitmap; 
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bitmap = new Bitmap(300, 300);
            g = Graphics.FromImage(bitmap); 
            this.pictureBox1.Image = bitmap;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("确定签名完成了吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); 
            if (result == DialogResult.Cancel) 
                return; 
            else 
            { 
               string  signaturePath = DateTime.Now.ToString("yyMMddHHmmssms") + ".jpg"; 
                compressionPicture(bitmap, 300, 300, signaturePath, ImageFormat.Jpeg); 
                //this.Close();
            } 
        }
        private void compressionPicture(Bitmap bmap, int newWidth, int newHeight, string saveFileName, ImageFormat format) 
        { 
            Bitmap newImg = new Bitmap(newWidth, newHeight); 
            Rectangle srcRec = new Rectangle(0, 0, bmap.Width, bmap.Height); 
            Rectangle destRec = new Rectangle(0, 0, newWidth, newHeight); 
            Graphics g = Graphics.FromImage(newImg);
            g.DrawImage(bmap, destRec, srcRec, GraphicsUnit.Pixel); 
            newImg.Save(saveFileName, format); 


        } 
    }
}


为什么保存图片全是黑的啊
求高手指点
[解决办法]
Graphics g = Graphics.FromImage(newImg);
g.Clear(Color.White);  

热点排行