调整图像大小。 。 。 。

发布于 2024-08-15 14:57:22 字数 1684 浏览 3 评论 0原文

您好,非常感谢,我在下面有一个代码,在此代码中我将上传一张图片。将其转换为 字节代码并将其存储在数据库中..并在gridview中检索它..事情是之前的 将其转换为字节代码我想调整它的大小你可以告诉我我应该在这里插入什么代码...非常感谢.. ...

protected void btnUpload_Click(object sender, EventArgs e)
{
 string strID= txtid.Text.ToString();
 string strImageName = txtName.Text.ToString();
 if (FileUpload1.PostedFile != null && 
     FileUpload1.PostedFile.FileName != "")
  {


   byte[] imageSize = new byte
                 [FileUpload1.PostedFile.ContentLength];

  HttpPostedFile uploadedImage = FileUpload1.PostedFile;
  uploadedImage.InputStream.Read
     (imageSize, 0, (int)FileUpload1.PostedFile.ContentLength);

 // Create SQL Connection 
  SqlConnection con = new SqlConnection("user id=sa;password=Zoomin@123;database=salary_db;server=192.168.1.100");


 // Create SQL Command 

 SqlCommand cmd = new SqlCommand();
 cmd.CommandText = "INSERT INTO image1(ID,ImageName,Image)" +
                   " VALUES (@ID,@ImageName,@Image)";
 cmd.CommandType = CommandType.Text;
 cmd.Connection = con;


 SqlParameter ID = new SqlParameter
                   ("@ID", SqlDbType.VarChar, 50);
 ID.Value = strID.ToString();
 cmd.Parameters.Add(ID);

 SqlParameter ImageName = new SqlParameter
                     ("@ImageName", SqlDbType.VarChar, 50);
 ImageName.Value = strImageName.ToString();
 cmd.Parameters.Add(ImageName);

 SqlParameter UploadedImage = new SqlParameter
               ("@Image", SqlDbType.Image, imageSize.Length);
 UploadedImage.Value = imageSize;
 cmd.Parameters.Add(UploadedImage);
 con.Open();
 int result = cmd.ExecuteNonQuery();
 con.Close();
 if (result > 0)
 lblMessage.Text = "File Uploaded";

 GridView1.DataBind();

 }}

hi thanks a lot i have one code below in this code ill upload one image . convert it into
byte code and store it in database .. and retrive it in gridview .. the thing is before
converting it into byte code i want to resize it could u plz tell me what code i should insert here ... thanks a lot .. ...

protected void btnUpload_Click(object sender, EventArgs e)
{
 string strID= txtid.Text.ToString();
 string strImageName = txtName.Text.ToString();
 if (FileUpload1.PostedFile != null && 
     FileUpload1.PostedFile.FileName != "")
  {


   byte[] imageSize = new byte
                 [FileUpload1.PostedFile.ContentLength];

  HttpPostedFile uploadedImage = FileUpload1.PostedFile;
  uploadedImage.InputStream.Read
     (imageSize, 0, (int)FileUpload1.PostedFile.ContentLength);

 // Create SQL Connection 
  SqlConnection con = new SqlConnection("user id=sa;password=Zoomin@123;database=salary_db;server=192.168.1.100");


 // Create SQL Command 

 SqlCommand cmd = new SqlCommand();
 cmd.CommandText = "INSERT INTO image1(ID,ImageName,Image)" +
                   " VALUES (@ID,@ImageName,@Image)";
 cmd.CommandType = CommandType.Text;
 cmd.Connection = con;


 SqlParameter ID = new SqlParameter
                   ("@ID", SqlDbType.VarChar, 50);
 ID.Value = strID.ToString();
 cmd.Parameters.Add(ID);

 SqlParameter ImageName = new SqlParameter
                     ("@ImageName", SqlDbType.VarChar, 50);
 ImageName.Value = strImageName.ToString();
 cmd.Parameters.Add(ImageName);

 SqlParameter UploadedImage = new SqlParameter
               ("@Image", SqlDbType.Image, imageSize.Length);
 UploadedImage.Value = imageSize;
 cmd.Parameters.Add(UploadedImage);
 con.Open();
 int result = cmd.ExecuteNonQuery();
 con.Close();
 if (result > 0)
 lblMessage.Text = "File Uploaded";

 GridView1.DataBind();

 }}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

俯瞰星空 2024-08-22 14:57:22

您可以使用以下函数:

public void ResizeImage(double scaleFactor, Stream fromStream, Stream toStream)
{
    using (var image = Image.FromStream(fromStream))
    {
        var newWidth = (int)(image.Width * scaleFactor);
        var newHeight = (int)(image.Height * scaleFactor);
        using (var thumbnailBitmap = new Bitmap(newWidth, newHeight))
        using (var thumbnailGraph = Graphics.FromImage(thumbnailBitmap))
        {
            thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
            thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;
            thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
            var imageRectangle = new Rectangle(0, 0, newWidth, newHeight);
            thumbnailGraph.DrawImage(image, imageRectangle);
            thumbnailBitmap.Save(toStream, image.RawFormat);
        }
    }
}

参数的名称应该非常不言自明。

You could use the following function:

public void ResizeImage(double scaleFactor, Stream fromStream, Stream toStream)
{
    using (var image = Image.FromStream(fromStream))
    {
        var newWidth = (int)(image.Width * scaleFactor);
        var newHeight = (int)(image.Height * scaleFactor);
        using (var thumbnailBitmap = new Bitmap(newWidth, newHeight))
        using (var thumbnailGraph = Graphics.FromImage(thumbnailBitmap))
        {
            thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
            thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;
            thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
            var imageRectangle = new Rectangle(0, 0, newWidth, newHeight);
            thumbnailGraph.DrawImage(image, imageRectangle);
            thumbnailBitmap.Save(toStream, image.RawFormat);
        }
    }
}

The name of the parameters should be pretty self-explanatory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文