设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 2764|回复: 13
打印 上一主题 下一主题

[帮助] JPG图片处理 [已解决]

[复制链接]
1#
发表于 2008-8-5 14:35:28 | 显示全部楼层
生成缩微图,.NET很容易解决,但Acc就显得很困难了。
2#
发表于 2008-8-5 14:59:28 | 显示全部楼层
自己根据实际需要更改一下:

C#







  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. using System.Drawing;
  12. /// <summary>
  13.        /// 生成缩略图
  14.        /// </summary>
  15.        /// <param name="originalImagePath">源图路径(物理路径)</param>
  16.        /// <param name="thumbnailPath">缩略图路径(物理路径)</param>
  17.        /// <param name="width">缩略图宽度</param>
  18.        /// <param name="height">缩略图高度</param>
  19.        /// <param name="mode">生成缩略图的方式</param>
  20.        public void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
  21.        {
  22.            System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
  23.            int towidth = width;
  24.            int toheight = height;
  25.            int x = 0;
  26.            int y = 0;
  27.            int ow = originalImage.Width;
  28.            int oh = originalImage.Height;
  29.            switch (mode)
  30.            {
  31.                case "HW"://指定高宽缩放(可能变形)               
  32.                    break;
  33.                case "W"://指定宽,高按比例                    
  34.                    toheight = originalImage.Height * width / originalImage.Width;
  35.                    break;
  36.                case "H"://指定高,宽按比例
  37.                    towidth = originalImage.Width * height / originalImage.Height;
  38.                    break;
  39.                case "Cut"://指定高宽裁减(不变形)               
  40.                    if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
  41.                    {
  42.                        oh = originalImage.Height;
  43.                        ow = originalImage.Height * towidth / toheight;
  44.                        y = 0;
  45.                        x = (originalImage.Width - ow) / 2;
  46.                    }
  47.                    else
  48.                    {
  49.                        ow = originalImage.Width;
  50.                        oh = originalImage.Width * height / towidth;
  51.                        x = 0;
  52.                        y = (originalImage.Height - oh) / 2;
  53.                    }
  54.                    break;
  55.                default:
  56.                    break;
  57.            }
  58.            //新建一个bmp图片
  59.            System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
  60.            //新建一个画板
  61.            Graphics g = System.Drawing.Graphics.FromImage(bitmap);
  62.            //设置高质量插值法
  63.            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
  64.            //设置高质量,低速度呈现平滑程度
  65.            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  66.            //清空画布并以透明背景色填充
  67.            g.Clear(Color.Transparent);
  68.            //在指定位置并且按指定大小绘制原图片的指定部分
  69.            g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
  70.                new Rectangle(x, y, ow, oh),
  71.                GraphicsUnit.Pixel);
  72.            try
  73.            {
  74.                //以jpg格式保存缩略图
  75.                bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
  76.            }
  77.            catch (System.Exception e)
  78.            {
  79.                throw e;
  80.            }
  81.            finally
  82.            {
  83.                originalImage.Dispose();
  84.                bitmap.Dispose();
  85.                g.Dispose();
  86.            }
  87.        }
复制代码


VB.Net

  1. Imports System
  2. Imports System.Data
  3. Imports System.Configuration
  4. Imports System.Collections
  5. Imports System.Web
  6. Imports System.Web.Security
  7. Imports System.Web.UI
  8. Imports System.Web.UI.WebControls
  9. Imports System.Web.UI.WebControls.WebParts
  10. Imports System.Web.UI.HtmlControls
  11. Imports System.Drawing

  12. '''<summary>
  13. '''生成缩略图
  14. '''</summary>
  15. '''<param name="originalImagePath">源图路径(物理路径)</param>
  16. '''<param name="thumbnailPath">缩略图路径(物理路径)</param>
  17. '''<param name="width">缩略图宽度</param>
  18. '''<param name="height">缩略图高度</param>
  19. '''<param name="mode">生成缩略图的方式</param>
  20. Sub MakeThumbnail(ByVal originalImagePath As String, ByVal thumbnailPath As String, ByVal width As Integer, ByVal height As Integer, ByVal mode As String)
  21.     Dim originalImage As System.Drawing.Image = System.Drawing.Image.FromFile(originalImagePath)
  22.     Dim towidth As Integer = width
  23.     Dim toheight As Integer = height
  24.     Dim x As Integer = 0
  25.     Dim y As Integer = 0
  26.     Dim ow As Integer = originalImage.Width
  27.     Dim oh As Integer = originalImage.Height
  28.     Select Case mode
  29.         Case "HW" '指定高宽缩放(可能变形)               
  30.         Case "W" '指定宽,高按比例                    
  31.             toheight = originalImage.Height * width / originalImage.Width
  32.         Case "H" '指定高,宽按比例
  33.             towidth = originalImage.Width * height / originalImage.Height
  34.         Case "Cut" '指定高宽裁减(不变形)               
  35.             If CDbl(originalImage.Width) / CDbl(originalImage.Height) > CDbl(towidth) / CDbl(toheight) Then
  36.                 oh = originalImage.Height
  37.                 ow = originalImage.Height * towidth / toheight
  38.                 y = 0
  39.                 x = (originalImage.Width - ow) / 2
  40.             Else
  41.                 ow = originalImage.Width
  42.                 oh = originalImage.Width * height / towidth
  43.                 x = 0
  44.                 y = (originalImage.Height - oh) / 2
  45.             End If
  46.         Case Else
  47.     End Select
  48.     '新建一个bmp图片
  49.     Dim bitmap As New System.Drawing.Bitmap(towidth, toheight)
  50.     '新建一个画板
  51.     Dim g As Graphics = System.Drawing.Graphics.FromImage(bitmap)
  52.     '设置高质量插值法
  53.     g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High
  54.     '设置高质量,低速度呈现平滑程度
  55.     g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality
  56.     '清空画布并以透明背景色填充
  57.     g.Clear(Color.Transparent)
  58.     '在指定位置并且按指定大小绘制原图片的指定部分
  59.     g.DrawImage(originalImage, New Rectangle(0, 0, towidth, toheight), New Rectangle(x, y, ow, oh), GraphicsUnit.Pixel)
  60.     Try
  61.         '以jpg格式保存缩略图
  62.         bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg)
  63.     Catch e As System.Exception
  64.         Throw e
  65.     Finally
  66.         originalImage.Dispose()
  67.         bitmap.Dispose()
  68.         g.Dispose()
  69.     End Try
  70. End Sub
复制代码
3#
发表于 2008-8-5 16:57:02 | 显示全部楼层
其实AcdSee就是个很好的工具,何必舍近求远呢?
4#
发表于 2008-9-22 07:05:10 | 显示全部楼层
上面提供的代码生成com类,acc可以引用使用的。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-5-23 00:31 , Processed in 0.096710 second(s), 27 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表