目录

总览

图像尺寸调整算法

如何在网页上使用控件?

如何从控件中获取上传的图像?

源代码

参考文献


总览

我们在图像大小调整和裁剪方面存在长期问题。我们的问题是,如果我们在服务器端调整大小,图像质量被破坏并且我们无法根据宽高比进行裁剪,因为上载的图像可以是任何大小和尺寸。对于B2B类型的项目,我们面临很多问题,因为网站用户需要上传图片,他们没有自己调整大小,也没有上传大图片来发布帖子,这使网站加载缓慢。

最后,我做了一个控件,该控件将上传的图像强制调整为所需的尺寸,并保持原始图像及其质量的正确比例。之后,用户可以选择自己的裁剪部分。

这是用户可以在其中进行裁剪的画布:

裁剪后,他们可以查看裁剪后的图像,并有机会再次进行其他裁剪,并按比例上传所需的尺寸(保持我为图像固定的长宽比)。

上传图像后,它将显示上传图像的预览,而无需刷新页面。

图像尺寸调整算法

所有过程都是使用JavaScriptJQueryAJAX完成的。因此,无需回发页面或刷新页面,并且此调整大小、裁剪、上传过程现在更加专业。我可以设置纵向和横向图像比例来进行裁剪,而不会扭曲图像的当前比例。为此,我开发了自己的调整大小算法并使用JavaScript实现。这是JavaScript格式的算法。

function ImageResizer(canvasWidth, canvasHeight, sourceWidth,sourceHeight, destinationX, destinationY, destinationWidth, destinationHeight) {var canvasRatio = canvasWidth / canvasHeight;var sourceRatio = sourceWidth / sourceHeight;if (sourceWidth > canvasWidth || sourceHeight > canvasHeight) {//if the image does not fit into the required canvasif (canvasRatio >= 1) {//if the canvas is landscapeif (sourceRatio <= 1) {//if the image is portraitdestinationHeight = canvasHeight;destinationWidth = destinationHeight * sourceRatio;destinationX = (canvasWidth - destinationWidth) / 2;destinationY = 0;}else if (sourceRatio > 1) {//if the image is landscapeif (canvasRatio < sourceRatio) {//make the landscape image fit inside the required Canvas.//In this case, ImageX is bigger than//canvasWidth & ImageY is smaller than canvasHeightdestinationWidth = canvasWidth;destinationHeight = destinationWidth / sourceRatio;destinationX = 0;destinationY = (canvasHeight - destinationHeight) / 2;} else if (canvasRatio >= sourceRatio) {//In this case, ImageY is bigger than canvasHeight &//ImageX is smaller than canvasWidthdestinationHeight = canvasHeight;destinationWidth = destinationHeight * sourceRatio;destinationX = (canvasWidth - destinationWidth) / 2;destinationY = 0;}}}else if (canvasRatio < 1) {//if the canvas is portraitif (sourceRatio >= 1) {//if the image is landscapedestinationWidth = canvasWidth;destinationHeight = destinationWidth / sourceRatio;destinationX = 0;destinationY = (canvasHeight - destinationHeight) / 2;}else if (sourceRatio < 1) {//if the image is portraitif (canvasRatio > sourceRatio) {//make the portrait image fit inside the required Canvas.//In this case, ImageY is bigger than//canvasHeight & ImageX is small than canvasWidthdestinationHeight = canvasHeight;destinationWidth = destinationHeight * sourceRatio;destinationX = (canvasWidth - destinationWidth) / 2;destinationY = 0;} else if (canvasRatio <= sourceRatio) {//In this case, ImageX is bigger than canvasWidth &//ImageY is smaller than canvasHeightdestinationWidth = canvasWidth;destinationHeight = destinationWidth / sourceRatio;destinationX = 0;destinationY = (canvasHeight - destinationHeight) / 2;}}}}else {//image will directly fit inside the canvasdestinationWidth = sourceWidth;destinationHeight = sourceHeight;destinationX = (canvasWidth - sourceWidth) / 2;destinationY = (canvasHeight - sourceHeight) / 2;}console.log("img.width=" + sourceWidth + " img.height=" +sourceHeight + " destinationX=" + destinationX + " destinationY=" +destinationY + " destinationWidth=" + destinationWidth +" destinationHeight=" + destinationHeight);
}

要了解该算法,您必须考虑下图:

得到所有适当的参数后,要将图像绘制到画布上,您必须将值传递给Canvas2D API中命名为CanvasRenderingContext2D.drawImage()JavaScript方法。

除了调整器算法之外,我还应用了一个名为jquery.Jcrop.jsJQuery裁剪API

我已经在Github上上传了这个控件的所有源代码,以获得其他程序员的帮助。我现在显示控件的文件夹结构:

源代码结构

F-ImageUploader是包含了所有的控制所需的文件的文件夹。

  1. ImageResizeNCrop.js包含所有用于调整大小、裁剪和保存的JavaScript代码。
  2. 一个ASP.NET处理程序文件(HandlerImageUploader.ashx)用于通过Ajax保存该文件。
  3. CMS_ImageResizeNCropp_CS.aspxCMS_ImageResizeNCropp_VB.aspx是控件的容器,它们将在fancybox iframe中打开。
  4. ASP.NET自定义控制器Control_CMS_ImageResizeNCropp_CS.ascxControl_CMS_ImageResizeNCropp_VB.ascx用于通过从VS解决方案资源管理器中拖动来快速插入并播放控件。
  5. 图像将保存在/Admin/Content文件夹中。

在本文中,我将不解释每一个代码。如果您是ASP.NETJavaScript开发人员,则可以从我在这里解释的提示和要点轻松地理解源代码。

如何在网页上使用控件?

在这里,WidthHeight你需要的图像尺寸。图像将根据该比例进行裁剪。ButtonName是您需要在按钮上显示的文本。另外,您可以按CssClass属性将CSS传递给该控件。

如何从控件中获取上传的图像?

首先,看看控制器标记:

<img data-attachment-id="1262"data-permalink="https://debchy.wordpress.com/2017/07/29/
create-asp-net-control-for-image-resizer-and-cropper-keeping-aspect-ratio/capture-2/"data-orig-file="https://debchy.files.wordpress.com/2017/07/capture1.png"data-orig-size="656,274" data-comments-opened="1"data-image-meta="{"aperture":"0","credit":"","camera":"","caption":"","created_timestamp":"0","copyright":"","focal_length":"0","iso":"0","shutter_speed":"0","title":"","orientation":"0"}" data-image-title="Capture"data-image-description=""data-medium-file="https://debchy.files.wordpress.com/2017/07/capture1.png?w=300"data-large-file="https://debchy.files.wordpress.com/2017/07/capture1.png?w=656"class="alignnone size-full wp-image-1262"src="https://debchy.files.wordpress.com/2017/07/capture1.png?w=809" alt="Capture"srcset="https://debchy.files.wordpress.com/2017/07/capture1.png 656w,
https://debchy.files.wordpress.com/2017/07/capture1.png?w=150 150w,
https://debchy.files.wordpress.com/2017/07/capture1.png?w=300 300w"sizes="(max-width: 656px) 100vw, 656px"   />

这是一个名为hdnResultImage的隐藏字段。您可以从控件对象访问该字段,因为隐藏字段是public属性。

源代码

  • https://github.com/debchy/ResizeNCrop

参考文献

  1. Javascript drawImage https://developer.mozilla.org/en/docs/Web/API/CanvasRenderingContext2D/drawImage
  2. Jquery Cropper API: http://jcrop.com

创建用于图像大小调整和裁剪器保持纵横比的ASP.NET控件相关推荐

  1. Python+Opencv图像处理新手入门教程(二):颜色空间转换,图像大小调整,灰度直方图

    一步一步来吧 上一节:Python+Opencv图像处理新手入门教程(一):介绍,安装与起步 1.什么是图像 对于计算机而言,图像的本质是一个由像素点构成的矩阵. 例如我们用肉眼很容易分辨一辆汽车的后 ...

  2. TensorFlow学习笔记之图像大小调整(最全可运行的代码)

    一.图像编码处理 在之前的章节中提到一张RGBRGBRGB色彩模式的图像可以看成一个三维矩阵,矩阵中的每一个数表示了图像上不同位置,不同颜色的亮度.然而图像在存储时并不是直接记录这些矩阵中的数字,而是 ...

  3. tensorflow入门之---------图像大小调整

    在深度学习的过程中,往往对输入图像的大小需要进行一个调整,特别是对于拥有全连接层的神经网络,因为全连接层的节点数是固定的.因此,为了满足全连接层的一个输入的要求,我们可以利用tensorflow自带的 ...

  4. 使用TensorFlow进行常用的图像处理-图像转为矩阵以及图像大小调整

    图像编码处理 将图像转为一个三维矩阵,并使用三维矩阵形成一个图像: import tensorflow as tf import matplotlib.pyplot as plt# 读取原始图像数据 ...

  5. 使用 ASP+ DataGrid 控件来创建主视图/详细资料视图

    Nikhil Kothari Microsoft Corporation 2000年8月 简介 Microsoft® Visual Studio.NET 的下一发行版包括 DataGrid Web 控 ...

  6. php 修改图像大小,PHP图像大小调整

    据我所知,您无法在上传图像之前调整图像大小. (我可能错了!)但是,当您上传图像时,它会进入一个临时文件.您可以调整临时图像的大小,并将调整后的图像复制到其最终目标. 因为(似乎)你想保持宽度不变,所 ...

  7. 调整图像大小调整图片大小

    B = imresize(A,scale) B = imresize(A,[numrows numcols]) [Y,newmap] = imresize(X,map,___) ___ = imres ...

  8. android 画布裁剪,一种基于Android系统对UI控件进行轮廓剪裁及美化的方法与流程...

    本发明涉及Android应用的技术领域,特别涉及一种基于Android系统对UI控件进行轮廓剪裁及美化的方法. 背景技术: 目前,随着智能电视的普及,Android应用层出不穷,而那些表现形式单一.传 ...

  9. android加一减一控件,Android的步进器(增加/减少值)控件?

    用于Android的步进器(增/减值)控件? 没有像Android中的Stepper这样的内置控件 但是您可以尝试这种方式 android:layout_width="wrap_conten ...

最新文章

  1. Screen 操作快捷键
  2. Python 【企鹅号、QQ看点】短视频的自动上传与发布实例演示,同时支持抖音、快手、哔哩哔哩、小红书、微视、西瓜视频、视频号等平台的视频自动化同步发布
  3. 接口请求时params与data的区别
  4. python之shutil模块
  5. LeetCode 2086. 从房屋收集雨水需要的最少水桶数(贪心)
  6. NUC1011 Financial Management【数学计算+水题】
  7. 数据结构-—树的存储结构
  8. postfix+dovecot+maildrop+mailscanner+clamav+spamassassin搭建rhel6平台邮件服务器
  9. 缠论中枢python源码_缠论中枢主图指标 源码 通达信 贴图 自动画笔
  10. 掌握到胃-奈氏图与伯德图的绘制
  11. 基于django的视频点播网站开发-step15-项目部署
  12. Android Studio Logcat 左边的工具栏不显示了 Show Toolbar
  13. python colormap_Python matplotlib的使用并自定义colormap的方法
  14. 方舟加载服务器就显示stm,方舟生存进化是不是stm
  15. 典型的人工神经网络由很多层构成,但不包括
  16. 测试Unity中常用代码的运行所需时间:循环和函数篇 #性能测试 # for,foreach,while循环 #委托事件
  17. KICAD批量修改原理图(.sch)中的字段
  18. 巨控GRM110无线通信模块
  19. cobol .cpy文件_Visual COBOL R3:“使传统的COBOL能够部署在JVM或.NET上”。
  20. 狂神。Mybatis学习。

热门文章

  1. 服务器java项目转移到另一个盘_将svn的项目从一台服务器转移到另外一台服务器...
  2. php oauth 扩展,PHP扩展之Web服务(一)——OAuth
  3. 值得电商美工借鉴的购物APP页面设计,让人无法自拔
  4. 万圣节海报设计没有思路?看看这些有趣的万圣节狂欢是如何完成的!
  5. 国庆海报设计适合哪些精品背景纹理?
  6. 超简单炫彩抽象线条感海报PSD分层素材,一切变得简单!
  7. web漏洞扫描工具_如何使用Arachni扫描web漏洞
  8. [译转] eBPF 概念和基本原理
  9. 利用scp在windows和linux之间进行文件和文件夹的数据拷贝
  10. 笔记:网络管理与检测命令