保存前调整C#Windows应用程序图像大小

时间:2014-04-17 07:38:32

标签: sql-server

我有此代码将Pic保存到我的Solution Explorer Images文件夹。

private void btnUploadImage_Click(object sender, EventArgs e)
        {
            //The String used to store the location of the file that is currently loaded in the picture box picFile
            String location;

            //The String used to store the name of the file that is currently loaded in the picture box picFile
            String fileName;
            ofdImageUpload.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif";
            //Showing the fileopen dialog box
            ofdImageUpload.ShowDialog();
            //showing the image opened in the picturebox
            imgCapture.Image = new Bitmap(ofdImageUpload.FileName);
            //storing the location of the pic in variable
            location = ofdImageUpload.FileName;
            txtImgLocation.Text = location;
            //storing the filename of the pic in variable
            fileName = ofdImageUpload.SafeFileName;
            //pictureboxImage.Image.Save();
            imgCapture.SizeMode = PictureBoxSizeMode.StretchImage;

            if (imgCapture.Image != null)
            {
                lblHiddenMsg.Text = "";
            }    
        }    


 private void InsertGatepassEntry(int RowId)
        {
            string ContName = txtContName.Text.Trim();
            string ContAdd = richtxtContAddress.Text.Trim();
            string VisitorName = txtEmpName.Text.Trim();
            string VisitorAdd = txtEmpAddress.Text.Trim();
            string VisitorFathersName = txtEmpFatherName.Text.Trim();
            string VisitorAge = txtEmpAge.Text.Trim();
            string VisitorEsi = txtEsi.Text.Trim();
            string VisitorContact = txtEmpContactNo.Text.Trim();
            string VisitorBloodGrp = comboxBloodGroup.SelectedText.Trim();
            string VisitorIssueDate = dtpEmpDateOfIssue.Text.Trim();    

            string imagename = ofdImageUpload.SafeFileName;

            if (imagename != null || imagename != "") //Check If image is Selected from Computer's Hard Drive
            {
                if (imgCapture.Image != null)
                {
                    //string imagepath = ofdImageUpload.FileName;
                    //string picname = imagepath.Substring(imagepath.LastIndexOf('\\'));
                    string picname = imagename;
                    string path = Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("bin"));
                    Bitmap imgImage = new Bitmap(imgCapture.Image);    //Create an object of Bitmap class/
                    //string fullPathName = path + "Images" + picname;

                    imgImage.Save(path + "Images\\" + txtEmpName.Text + txtEsi.Text + ".jpg");    
                    string Image = "Images\\" + txtEmpName.Text + txtEsi.Text + ".jpg";

                    string GatepassNo = LoadLastGatepassNo();
                    switch (Contractor.InsertGatepassEntry(RowId, ContName, ContAdd, VisitorName, VisitorAdd, VisitorFathersName, VisitorAge, VisitorEsi, VisitorContact, VisitorBloodGrp, VisitorIssueDate, Image, GatepassNo))
                    {

                        case ProjectCreateStatus.Insertrow:

                            lblMessage.Text = "Information inserted successfully!";
                            lblMessage.ForeColor = System.Drawing.Color.Green;
                            lblGatepassNo.Text = GatepassNo;
                            break;    
                    }
                }
                else
                {
                    lblHiddenMsg.Visible = true;
                    lblHiddenMsg.Text = "Please capture or browse an image First";    
                }
            }
            else //image is directly uploding from Webcam Capture
            {    
                string Image = lblHiddenMsg.Text;    
                string GatepassNo = LoadLastGatepassNo();
                switch (Contractor.InsertGatepassEntry(RowId, ContName, ContAdd, VisitorName, VisitorAdd, VisitorFathersName, VisitorAge, VisitorEsi, VisitorContact, VisitorBloodGrp, VisitorIssueDate, Image, GatepassNo))
                {    
                    case ProjectCreateStatus.Insertrow:

                        lblMessage.Text = "Information inserted successfully!";
                        lblMessage.ForeColor = System.Drawing.Color.Green;
                        lblGatepassNo.Text = GatepassNo;
                        break;    
                }    
            }
        }    


 private void bntCapture_Click(object sender, EventArgs e)
        {
            imgCapture.Image = imgVideo.Image;
            Bitmap b = new Bitmap(imgCapture.Image);
            string path = Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("bin"));

            b.Save(path + "Images\\" + txtEmpName.Text + txtEsi.Text + ".jpg");
            lblHiddenMsg.Text = path + "Images\\" + txtEmpName.Text + txtEsi.Text + ".jpg";    
        }

现在我想将上传的Pic保存到我的图像文件夹中,保存为特定尺寸,如250x250。有人可以帮忙吗? Windows应用程序C#中的新功能。

2 个答案:

答案 0 :(得分:0)

要调整图片大小,请使用:

public static Image resizeImage(Image imgToResize, Size size)
    {
       return (Image)(new Bitmap(imgToResize, size));
    }

    yourImage = resizeImage(yourImage, new Size(250,250));

答案 1 :(得分:0)

您可以添加“Size”类作为Bitmap构造函数的参数来调整图像大小 我为你做了这个功能,让你很容易希望能帮到你

public static Image resizeImage(Image imgToResize, Size size)
{
   return (Image)(new Bitmap(imgToResize, size));
}

yourImage = resizeImage(yourImage, new Size(250,250));