C#图片上传展现代码(含部分DNN代码)

本文档提供了C#实现的图片上传功能的前后端代码示例,包括前端的文件选择与提交,以及后台的接收与存储处理。同时,也展示了如何在前端进行图片的展示。适用于DotNetNuke(DNN)平台。

上传代码:

前端

<input type="file" id="Txb_PicUrl" runat="server"  title="浏览" size="34" />
            <div class="DetailTable_NoticePoint" style="display:inline; ">要求:二寸免冠近照;jpg、png、gif格式;大小1M以下</div>

后台

Jz_ProductController objJz_Products = new Jz_ProductController();
                Jz_ProductInfo objJz_Product = new Jz_ProductInfo();

                // 文件客户端路径
                HtmlInputFile HiFile = (HtmlInputFile)this.FindControl("Txb_PicUrl");

                // 上传的图片文件名
                string strImageFilename="";

                if (HiFile == null || HiFile.PostedFile.FileName.Length == 0)
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('请上传照片文件...')", true);
                    return;
                }
                else
                {
                    strImageFilename = HiFile.PostedFile.FileName;

                    //获取文件扩展名
                    string strFileName_Kz = HiFile.PostedFile.ContentType.ToString();
                    strFileName_Kz = strFileName_Kz.Substring(strFileName_Kz.Length - 4);
                    if (strFileName_Kz == "jpeg" || strFileName_Kz == "/png" || strFileName_Kz == "/gif")
                    {
                    }
                    else
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('照片只允许上传JPG、PNG、GIF格式的文件...')", true);
                        return;
                    }
                    

                    //判断文件大小,不允许超过1M
                    if (HiFile.PostedFile.ContentLength > 1048576)
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('照片文件大小不超过1M...')", true);
                        return;
                    }
                    else
                    {
                        //新文件名=日期+原文件名
                        strImageFilename =  DateTime.Now.ToFileTime().ToString("") +strImageFilename ;
                    }
                }
                // 图像文件
                objJz_Product.PicUrl = strImageFilename;
                // 站点物理路径
                string strHostFilePath = Request.PhysicalApplicationPath;

                //strHostFilePath = strHostFilePath.Substring(0, strHostFilePath.LastIndexOf("\\"));
                //strHostFilePath = strHostFilePath.Substring(0, strHostFilePath.LastIndexOf("\\"));
                    
                // 拼合保存到服务的文件名
                strHostFilePath = strHostFilePath + "DesktopModules\\YourCompany.Jz_Product\\ProductPic\\";
                //Response.Write(strHostFilePath.ToString());
                //Response.End();
                HiFile.PostedFile.SaveAs(strHostFilePath + strImageFilename);
                    
                //相应的值赋给数据库字段
                objJz_Product.Category = ddlCategory.SelectedItem.Value;
                objJz_Product.ProductName = Txb_ProductName.Text;
                objJz_Product.ProductContent = Txb_ProductContent.Text;
                objJz_Product.Location = Txb_Location.Text;
                objJz_Product.Price = decimal.Parse(Txb_Price.Text.ToString());
                objJz_Product.ProductCreatedDate = DateTime.Now;
                objJz_Product.Attachment = Txb_Attachment.Text;
                //保存数据操作
                objJz_Products.Add_Product(objJz_Product);

展现代码:

前端

<td   align="center" valign="middle" >
            <asp:Image ID="img_Kszp" Width="300px" Height="200px" runat="server"/>
        </td>

后台

// 获取并显示产品图片
                    string strKszp = objJz_GetProduct.PicUrl.Trim();

                    // 绑定显示考生照片
                    if (strKszp != "")
                    {
                        ViewState["Kszp"] = strKszp;
                        string strImageUrl = "";
                        strImageUrl = Request.Url.AbsoluteUri;
                        strImageUrl = strImageUrl.Substring(0, strImageUrl.IndexOf("/", 7));
                        strImageUrl = strImageUrl + "/DesktopModules/YourCompany.Jz_Product/ProductPic/" + strKszp;
                        img_Kszp.ImageUrl = strImageUrl;
                    }


### C# 和 OpenCvSharp 的 DNN 开发 OpenCV 是一个强大的计算机视觉库,而其 .NET 绑定版本 OpenCvSharp 提供了类似的接口支持。通过 OpenCvSharp 中的 `Dnn` 命名空间可以加载预训练模型并执行推理操作[^1]。 以下是基于 C# 和 OpenCvSharp 实现深度学习的一个简单示例代码: #### 加载预训练模型 ```csharp using System; using OpenCvSharp; class Program { static void Main(string[] args) { string modelPath = "path_to_model.caffemodel"; // 替换为实际路径 string configPath = "path_to_config.prototxt"; // 替换为实际路径 using (var net = CvDnn.ReadNetFromCaffe(configPath, modelPath)) { Mat inputBlob = CvDnn.BlobFromImage(new Mat("input_image.jpg"), 1.0, new Size(224, 224), new Scalar(104, 117, 123)); net.SetInput(inputBlob); Mat detectionMat = net.Forward(); Console.WriteLine($"Output shape: {detectionMat.Size}"); } } } ``` 上述代码展示了如何使用 OpenCvSharp 调用预训练的 Caffe 模型进行图像分类的任务。其中的关键函数包括: - **ReadNetFromCaffe**: 使用指定的配置文件和权重文件初始化神经网络。 - **BlobFromImage**: 将输入图片转换成适合神经网络处理的形式。 - **SetInput**: 设置网络的输入数据。 - **Forward**: 执行前向传播计算,并返回输出结果[^2]。 #### 关于性能优化 为了提高运行效率,在部署阶段可以选择量化或者剪枝技术来减少模型大小以及提升速度。此外还可以利用 GPU 来加速运算过程,这通常需要安装 NVIDIA CUDA 工具包并与 OpenCV 配合工作[^3]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值