在不压缩的情况下显示特定尺寸的图像并获得特定尺寸的图像 [英] Display Image on particular size without compress and get image with particular size

查看:53
本文介绍了在不压缩的情况下显示特定尺寸的图像并获得特定尺寸的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的图像尺寸有问题。当我上传500 * 500尺寸或其他尺寸的图像时,在这种情况下我想要1024 * 768尺寸图像输出没有压缩。意味着,在图像背景上自动添加其他空间(作为黑色背景或其他)。怎么办,我有什么建议。



1我试图在固定尺寸的图像控制上显示图像。



.aspx



I have problem with image dimensions. when i have upload image with 500*500 dimensions or other, In this case i want 1024*768 dimensions Image output without compress. means, other space are automatic added (as a black background or other) on image background. How can do i, any suggest for that.

1st i have try to display image on image control with fixed size.

.aspx

<asp:Image ID="ImagePreview1" BorderColor="Aqua"
      BorderWidth="1px" runat="server"  Height="768px" Width="1024px" />

< br $>


aspx.cs



aspx.cs

protected void btnsave_Click(object sender, EventArgs e)
{
    string filepath = ImagePreview1.ImageUrl;
    using (WebClient client = new WebClient())
    {
        client.DownloadFile(filepath, Server.MapPath("~/imgprev/pic.jpg"));
    }

}



介于两者之间我也使用此代码。此代码用于压缩图像大小..


In between i have also working with this code. This code for compress image size..

System.Drawing.Image fullSizeImg = System.Drawing.Image.FromFile(imagePath);
var thumbnailImg = new Bitmap(1024, 768);
var thumbGraph = Graphics.FromImage(thumbnailImg);
thumbGraph.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
thumbGraph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
thumbGraph.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
var imageRectangle = new Rectangle(0, 0, 1024, 768);
thumbGraph.DrawImage(fullSizeImg, imageRectangle);
string targetPath = imagePath.Replace(Path.GetFileNameWithoutExtension(imagePath), Path.GetFileNameWithoutExtension(imagePath) + "-resize");
string SName = Path.GetFileName(targetPath);
string SImgName = ("~/imgprev/") + SName;
//string st = targetPath.ToString();
//string stArr = st.Split('\\')[8];

thumbnailImg.Save(targetPath, System.Drawing.Imaging.ImageFormat.Jpeg); //(A generic error occurred in GDI+) Error occur here !
Label1.Text = SImgName.ToString();

thumbnailImg.Dispose();
return targetPath;

推荐答案

ASPX



ASPX

<table>
           <tr>
               <td style="vertical-align: top">
                   <asp:Label ID="OriginalImageLabel" Font-Bold="true" runat="server">Original Image</asp:Label>
               </td>
               <td>
                   <asp:Image ID="Image" ImageUrl="~/Images/Images.jpg" runat="server" BorderColor="Aqua"

                       BorderWidth="1px" Height="500px" Width="500px" />
               </td>
           </tr>
           <tr>
               <td style="vertical-align: top">
                   <asp:Label ID="ResizeImageLabel" Font-Bold="true" Text="Re-Size Image" runat="server"></asp:Label>
               </td>
               <td>
                   <asp:Image ID="ResizeImage" runat="server" />
               </td>
           </tr>
           <asp:Button ID="ResizeButton" runat="server" Text="Resize" OnClick="ResizeButton_click" />
       </table>







ASPX.CS






ASPX.CS

<br />
using System;<br />
using System.Collections.Generic;<br />
using System.Drawing;<br />
using System.Drawing.Drawing2D;<br />
using System.Linq;<br />
using System.Web;<br />
using System.Web.UI;<br />
using System.Web.UI.WebControls;<br />
<br />
public partial class _Default : System.Web.UI.Page<br />
{<br />
    protected void Page_Load(object sender, EventArgs e)<br />
    {<br />
<br />
    }<br />
<br />
    #region Events<br />
    protected void ResizeButton_click(object sender,EventArgs e)<br />
    {<br />
        string path = Server.MapPath("~/Images");<br />
        System.Drawing.Image img = System.Drawing.Image.FromFile(string.Concat(path, "/images.jpg"));<br />
        img = resizeImage(img);<br />
        img.Save(string.Concat(path,"/new.jpg"));<br />
        ResizeImage.ImageUrl="~/Images/new.jpg";<br />
<br />
<br />
    }<br />
    #endregion<br />
<br />
    #region Private Methods<br />
<br />
<br />
    private System.Drawing.Image resizeImage(System.Drawing.Image img)<br />
    {<br />
        Bitmap b = new Bitmap(img);<br />
        System.Drawing.Image i = resizeImage(b, new Size(1024, 768));<br />
        return i;<br />
    }<br />
<br />
<br />
<br />
    private static System.Drawing.Image resizeImage(System.Drawing.Image imgToResize, Size size)<br />
    {<br />
        //Get the image current width<br />
        int sourceWidth = imgToResize.Width;<br />
        //Get the image current height<br />
        int sourceHeight = imgToResize.Height;<br />
<br />
        float nPercent = 0;<br />
        float nPercentW = 0;<br />
        float nPercentH = 0;<br />
        //Calulate  width with new desired size<br />
        nPercentW = ((float)size.Width / (float)sourceWidth);<br />
        //Calculate height with new desired size<br />
        nPercentH = ((float)size.Height / (float)sourceHeight);<br />
        <br />
<br />
        if (nPercentH < nPercentW)<br />
            nPercent = nPercentH;<br />
        else<br />
            nPercent = nPercentW;<br />
        //New Width<br />
        int destWidth = (int)(sourceWidth * nPercent);<br />
        //New Height<br />
        int destHeight = (int)(sourceHeight * nPercent);<br />
<br />
        Bitmap b = new Bitmap(destWidth, destHeight);<br />
        Graphics g = Graphics.FromImage((System.Drawing.Image)b);<br />
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;<br />
        // Draw image with new width and height<br />
        g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);<br />
        g.Dispose();<br />
<br />
        return (System.Drawing.Image)b;<br />
    }<br />
   <br />
  <br />
    #endregion<br />
}<br />
<br />
<br />


这篇关于在不压缩的情况下显示特定尺寸的图像并获得特定尺寸的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆