在ASP.NET Web应用程序中添加预览部分

huangapple go评论51阅读模式
英文:

Adding a preview section in ASP.NET web application

问题

在代码中,我有一个ASP按钮,当点击时,会在ASP图像框中显示已上传和调整大小的图像。是否可以在不点击“Show”按钮(Button1)的情况下预览图像?也就是说,是否可以直接使图像可见,而无需点击Button1?我正在使用Visual Studio 2010开发Web应用程序,它是一个ASP.NET Web应用程序,因此不支持任何JavaScript代码。

英文:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.IO;
using System.Drawing;
namespace imageResize1
{
    public partial class _Default : System.Web.UI.Page
    {
        string Strcon = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
                ProcessedImage();
        }
        private void ProcessedImage()
        {
            try
            {
                if (FileUpload1.HasFile)
                {
                    int length = 192;
                    int width = 192;
                    using (Bitmap sourceImage = new Bitmap(FileUpload1.PostedFile.InputStream))
                    {
                        using (Bitmap resizedImage = new Bitmap(length, width))
                        {
                            using (Graphics graphics = Graphics.FromImage(resizedImage))
                            {
                                graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                                graphics.SmoothingMode = SmoothingMode.HighQuality;
                                graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
                                graphics.DrawImage(sourceImage, 0, 0, length, width);
                            }
                            string resizedImagePath = Server.MapPath("~/Images/finalImage.png");
                            resizedImage.Save(resizedImagePath, ImageFormat.Png);
                            ImgPhoto.ImageUrl = "~/Images/finalImage.png";
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                string errorMessage = "An error occurred " + ex.Message;
            }
        }         
    }
}

In the code I have an asp button which on clicked shows the uploaded and resized image in the asp image box. Is it possible to preview the image without clicking the Show button (Button1)? i.e is it possible to make the image visible directly without clicking the button1? I am developing the web application in Visual Studio 2010. It is an ASP.NET Web Application. So it won't support any javascript code.

答案1

得分: 0

我们有一个用户选择文件,然后点击按钮上传文件,然后后台代码处理该文件,然后显示已处理的文件。

如果您不希望用户必须点击提交按钮来上传文件,那么您可以采用异步文件上传控件。当他们选择文件时,它将自动上传,无需提交按钮。这也将触发后台代码运行。然而,由于没有发生后端提交,因此后台代码可以处理文件,但无法更新图像控件。然而,异步文件上传器完成后可以触发客户端端的一小段代码。这段代码可以获取新的源路径并更新图像控件,或者您甚至可以使用客户端JavaScript代码点击/触发按钮来更新图像控件。

因此,您可以考虑采用并使用不同的文件上传控件。在ajaxtoolkit中有一个异步文件上传器,它应该与您现有的代码和站点兼容。

英文:

Well, we have a user selecting a file, then hitting a button to upload the file, then code behind processes that file, and then you display that processed file.

If you don't want the user to have to hit submit to up-load the file, then you can adopt a asynchronous file upload control. When they select the file, it will automatic up-load upon that selecting and no submit button is required. this will also trigger the code behind to run. However, since no post-back is occurring, that code behind can process the file, but it will not be able to update the image control. However, the async file uploader when done can trigger a client side bit of code. That bit of code can either get the new source path and update the image control, or you could even have the client side JavaScript code click/trigger a button to update the image control.

So, you could consider adopting and using a different file up-load control. There is a async file uploader in the ajaxtoolkit, which would and should be compatible with your existing code and site.

huangapple
  • 本文由 发表于 2023年6月1日 14:26:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76379184.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定