C# 我想使用ASP.NET C使用单个文件上载插入两个不同大小的图像#

C# 我想使用ASP.NET C使用单个文件上载插入两个不同大小的图像#,c#,asp.net,C#,Asp.net,我想插入两个不同大小的图像使用单一文件上传 我已经插入了一个图像大小为101,但我需要插入另一个51大小的同一用户的小图像 id BigImage SmallImage 1 mazhar.jpg NULL 2 mazhar.jpg NULL 3 12_n.jpg NULL 我需要的结果如下: id BigImage SmallImage 1 mazhar.jpg smallmazhar.jpg 2 mazhar.jpg smallmazhar.

我想插入两个不同大小的图像使用单一文件上传

我已经插入了一个图像大小为101,但我需要插入另一个51大小的同一用户的小图像

id  BigImage    SmallImage
 1  mazhar.jpg  NULL
 2  mazhar.jpg  NULL
 3  12_n.jpg    NULL
我需要的结果如下:

 id BigImage    SmallImage
 1  mazhar.jpg  smallmazhar.jpg
 2  mazhar.jpg  smallmazhar.jpg
 3  12_n.jpg    small12_n.jpg
C代码如下所示:

protected void Button1_Click(object sender, EventArgs e)
  {
     //http://forums.asp.net/t/1079883.aspx?PageIndex=1
    string Status = string.Empty;
    int id = 0;

    const int bmpW = 101;
    //New image target width

    const int bmpH = 101;
    //New Image target height

    bo.Para1 = FileUpload1.FileName.ToString();// Passing parameter

    if ((FileUpload1.HasFile))
    {
        //Clear the error label text
        lblError.Text = "";

        //Check to make sure the file to upload has a picture file format extention and set the target width and height

        if ((CheckFileType(FileUpload1.FileName)))
        {
            Int32 newWidth = bmpW;
            Int32 newHeight = bmpH;

            //Use the uploaded filename for saving without the '.' extension

            String upName = FileUpload1.FileName.Substring(0, FileUpload1.FileName.IndexOf("."));

            //Set the save path of the resized image, you will need this directory already created in your web site

       //   string filePath = "~/Upload/" + upName + ".jpg";

            bl.Insert_PhotoInfo(bo, out Status, out id);

            string filePath = Convert.ToString(id) + bo.Para1;

           FileUpload1.PostedFile.SaveAs(Request.ServerVariables["APPL_PHYSICAL_PATH"] + "Upload/" + filePath);

            //Create a new Bitmap using the uploaded picture as a Stream

            //Set the new bitmap resolution to 72 pixels per inch

            Bitmap upBmp = (Bitmap)Bitmap.FromStream(FileUpload1.PostedFile.InputStream);

            Bitmap newBmp = new Bitmap(newWidth, newHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);

            newBmp.SetResolution(72, 72);

            //Get the uploaded image width and height

            Double upWidth = upBmp.Width;

            Double upHeight = upBmp.Height;

            int newX = 0;
            //Set the new top left drawing position on the image canvas

            int newY = 0;

            Double reDuce;

            //Keep the aspect ratio of image the same if not 4:3 and work out the newX and newY positions

            //to ensure the image is always in the centre of the canvas vertically and horizontally

            if (upWidth > upHeight)
            {
                //Landscape picture
                reDuce = newWidth / upWidth;

                //calculate the width percentage reduction as decimal
                newHeight = ((Int32)(upHeight * reDuce));

                //reduce the uploaded image height by the reduce amount
                newY = ((Int32)((bmpH - newHeight) / 2));

                //Position the image centrally down the canvas
                newX = 0;
                //Picture will be full width
            }
            else if (upWidth < upHeight)
            {
                //Portrait picture
                reDuce = newHeight / upHeight;

                //calculate the height percentage reduction as decimal
                newWidth = ((Int32)(upWidth * reDuce));

                //reduce the uploaded image height by the reduce amount
                newX = ((Int32)((bmpW - newWidth) / 2));

                //Position the image centrally across the canvas
                newY = 0;
                //Picture will be full hieght
            }
            else if (upWidth == upHeight)
            {
                //square picture
                reDuce = newHeight / upHeight;

                //calculate the height percentage reduction as decimal
                newWidth = ((Int32)(upWidth * reDuce));

                //reduce the uploaded image height by the reduce amount
                newX = ((Int32)((bmpW - newWidth) / 2));
                //Position the image centrally across the canvas


                newY = ((Int32)((bmpH - newHeight) / 2));
                //Position the image centrally down the canvas
            }
            //Create a new image from the uploaded picture using the Graphics class

            //Clear the graphic and set the background colour to white

            //Use Antialias and High Quality Bicubic to maintain a good quality picture

            //Save the new bitmap image using 'Png' picture format and the calculated canvas positioning

            Graphics newGraphic = Graphics.FromImage(newBmp);

            try
            {
                newGraphic.Clear(Color.White);

                newGraphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

                newGraphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

                newGraphic.DrawImage(upBmp, newX, newY, newWidth, newHeight);

                newBmp.Save(MapPath(filePath), System.Drawing.Imaging.ImageFormat.Jpeg);

                //Show the uploaded resized picture in the image control
                Image1.ImageUrl = filePath;
                Image1.Visible = true;
            }
            catch (Exception ex)
            {
                string newError = ex.Message;
                lblError.Text = newError;
            }
            finally
            {
                upBmp.Dispose();
                newBmp.Dispose();
                newGraphic.Dispose();
            }
        }
        else
        {
            lblError.Text = "Please select a picture with a file format extension of either Bmp, Jpg, Jpeg, Gif or Png.";
        }
    }
}
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
//http://forums.asp.net/t/1079883.aspx?PageIndex=1
字符串状态=string.Empty;
int id=0;
常数int bmpW=101;
//新图像目标宽度
常数int bmpH=101;
//新图像目标高度
bo.Para1=FileUpload1.FileName.ToString();//传递参数
if((FileUpload1.HasFile))
{
//清除错误标签文本
lblError.Text=“”;
//检查以确保要上载的文件具有图片文件格式扩展名,并设置目标宽度和高度
if((检查文件类型(FileUpload1.FileName)))
{
Int32 newWidth=bmpW;
Int32 newHeight=bmpH;
//使用上载的文件名保存,不带扩展名“.”
字符串upName=FileUpload1.FileName.Substring(0,FileUpload1.FileName.IndexOf(“.”);
//设置已调整大小的图像的保存路径,则需要在网站中已创建此目录
//字符串filePath=“~/Upload/”+upName+”.jpg”;
bl.插入照片信息(bo、输出状态、输出id);
字符串filePath=Convert.ToString(id)+bo.Para1;
FileUpload1.PostedFile.SaveAs(Request.ServerVariables[“APPL_PHYSICAL_PATH”]+“Upload/”+filePath);
//使用上传的图片作为流创建新位图
//将新位图分辨率设置为每英寸72像素
位图upBmp=(位图)Bitmap.FromStream(FileUpload1.PostedFile.InputStream);
位图newBmp=新位图(newWidth、newHeight、System.Drawing.Imaging.PixelFormat.Format24bppRgb);
NEBMP.SetResolution(72,72);
//获取上传图像的宽度和高度
双向上宽度=向上宽度;
双仰角=上升高度;
int newX=0;
//在图像画布上设置新的左上角绘图位置
int newY=0;
双降;
//如果不是4:3,则保持图像的纵横比相同,并计算出newX和newY位置
//确保图像始终在画布的垂直和水平中心
如果(仰角>仰角)
{
//风景画
减少=新宽度/上升宽度;
//以十进制计算宽度减少百分比
新高度=((Int32)(仰角*下降));
//将上载的图像高度降低减少量
newY=((Int32)((bmpH-newHeight)/2));
//将图像放在画布的中心位置
newX=0;
//图片将是全宽的
}
否则,如果(上边<仰角)
{
//肖像画
减少=新高度/仰角;
//以十进制计算高度减少百分比
新宽度=((Int32)(上升宽度*减少));
//将上载的图像高度降低减少量
newX=((Int32)((bmpW-newWidth)/2));
//将图像放置在画布的中心位置
newY=0;
//图片将是完整的
}
else if(上升高度==上升高度)
{
//方格画
减少=新高度/仰角;
//以十进制计算高度减少百分比
新宽度=((Int32)(上升宽度*减少));
//将上载的图像高度降低减少量
newX=((Int32)((bmpW-newWidth)/2));
//将图像放置在画布的中心位置
newY=((Int32)((bmpH-newHeight)/2));
//将图像放在画布的中心位置
}
//使用Graphics类从上载的图片创建新图像
//清除图形并将背景色设置为白色
//使用抗锯齿和高质量双三次曲线来保持高质量的图片
//使用“Png”图片格式和计算的画布位置保存新位图图像
Graphics newGraphic=Graphics.FromImage(newBmp);
尝试
{
新图形。清晰(颜色。白色);
newGraphic.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
newGraphic.InterpolationMode=System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
newGraphic.DrawImage(upBmp、newX、newY、newWidth、newHeight);
newBmp.Save(MapPath(filePath)、System.Drawing.Imaging.ImageFormat.Jpeg);
//在图像控件中显示上载的已调整大小的图片
Image1.ImageUrl=文件路径;
Image1.可见=真;
}
捕获(例外情况除外)
{
字符串newError=ex.Message;
lblError.Text=newError;
}
最后
{
upBmp.Dispose();
newBmp.Dispose();
newGraphic.Dispose();
}
}
其他的
{
lblError.Text=“请选择文件格式扩展名为Bmp、Jpg、Jpeg、Gif或Png的图片。”;
}
}
}
试试这个
它应该适合您

请澄清您的问题。按照您的设置方式,您需要使用您想要的两种尺寸呼叫它两次。您可以将代码从按钮单击中取出,并将其放入自己的方法中,使用width和height参数,然后使用各种