GVKun编程网logo

php实现图片按比例截取的方法(php实现图片按比例截取的方法是)

13

在这里,我们将给大家分享关于php实现图片按比例截取的方法的知识,让您更了解php实现图片按比例截取的方法是的本质,同时也会涉及到如何更有效地.Net实现上传图片按比例自动缩小或放大的方法、C#图片按

在这里,我们将给大家分享关于php实现图片按比例截取的方法的知识,让您更了解php实现图片按比例截取的方法是的本质,同时也会涉及到如何更有效地.Net实现上传图片按比例自动缩小或放大的方法、C#图片按比例缩放、C#图片按比例缩放实例、C#图片按比例缩放的示例代码分享的内容。

本文目录一览:

php实现图片按比例截取的方法(php实现图片按比例截取的方法是)

php实现图片按比例截取的方法(php实现图片按比例截取的方法是)

本文实例讲述了PHP实现图片按比例截取的方法。分享给大家供大家参考,具体如下:

array("create"=>"ImageCreateFromjpeg","output"=>"imagejpeg","exn"=>".jpg"),"gif" => array("create"=>"ImageCreateFromGIF","output"=>"imagegif","exn"=>".gif"),"jpeg" => array("create"=>"ImageCreateFromjpeg","png" => array("create"=>"imagecreatefrompng","output"=>"imagepng","exn"=>".png"),"wbmp" => array("create"=>"imagecreatefromwbmp","output"=>"image2wbmp","exn"=>".wbmp") ); $imgtype = getimagesize($filename); $width = $imgtype[0]; $height = $imgtype[1]; $type = str_replace('image/','',$imgtype['mime']); $func_create = $all_type[$type]['create']; $func_output = $all_type[$type]['output']; $x = $y =0; if(($width * 100)>($height * 120)) { $newwidth = ceil($height * 120/100); $newheight = $height; $x = ($width-$newwidth)/2; } elseif(($width * 100)<($height * 120)) { $newheight = ceil($width * 100/120); $newwidth = $width; $y = ($height-$newheight)/2; } else { $newheight = $height; $newwidth = $width; } // Load $thumb = imagecreatetruecolor($newwidth,$newheight); $source = $func_create($filename); // Resize imagecopyresized($thumb,$source,$newwidth,$newheight,$newheight); // Output $func_output($thumb,'a.jpeg');

PS:这里再为大家推荐几款比较实用的图片处理工具供大家参考使用:

在线图片转换BASE64工具:

ICO图标在线生成工具:

在线Email邮箱图标制作工具:

在线图片格式转换(jpg/bmp/gif/png)工具:

更多关于PHP相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》、《》、《》、《》及《PHP常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

.Net实现上传图片按比例自动缩小或放大的方法

.Net实现上传图片按比例自动缩小或放大的方法

本文实例主要展示了.Net实现上传图片按比例自动缩小或放大的方法,是非常实用的功能。分享给大家供大家参考之用。具体方法如下:

//// <summary>
/// 按比例缩小图片,自动计算宽度
/// </summary>
/// <param name="strOldPic">源图文件名(包括路径)</param>
/// <param name="strNewPic">缩小后保存为文件名(包括路径)</param>
/// <param name="intHeight">缩小至高度</param>
public void SmallPicWidth(string strOldPic, string strNewPic, int intHeight)
{
  System.Drawing.Bitmap objPic, objNewPic;
  try
  {
 objPic = new System.Drawing.Bitmap(strOldPic);
 int intWidth = (intHeight / objPic.Height) * objPic.Width;
 objNewPic = new System.Drawing.Bitmap(objPic, intWidth, intHeight);
 objNewPic.Save(strNewPic);
  }
  catch (Exception exp) { throw exp; }
  finally
  {
 objPic = null;
 objNewPic = null;
  }
}
/**//// <summary>
/// 缩小图片
/// </summary>
/// <param name="strOldPic">源图文件名(包括路径)</param>
/// <param name="strNewPic">缩小后保存为文件名(包括路径)</param>
/// <param name="intWidth">缩小至宽度</param>
/// <param name="intHeight">缩小至高度</param>
public void SmallPic(string strOldPic, string strNewPic, int intWidth, int intHeight)
{
  System.Drawing.Bitmap objPic, objNewPic;
  try
  {
 objPic = new System.Drawing.Bitmap(strOldPic);
 objNewPic = new System.Drawing.Bitmap(objPic, intWidth, intHeight);
 objNewPic.Save(strNewPic);
  }
  catch (Exception exp)
 { throw exp; }
  finally
  {
 objPic = null;
 objNewPic = null;
  }
}

希望本文所述实例对大家的asp.net程序设计有一定的借鉴价值。

您可能感兴趣的文章:
  • .net实现裁剪网站上传图片的方法
  • .net中 发送邮件内容嵌入图片的具体实例
  • vb.net借助剪贴板将图片导入excel内
  • asp.net图片上传实例
  • ASP.net WebAPI 上传图片实例
  • .Net下二进制形式的文件(图片)的存储与读取详细解析
  • Asp.net图片上传实现预览效果的简单代码
  • asp.net上传图片并作处理水印与缩略图的实例代码
  • asp.net 图片超过指定大小后等比例压缩图片的方法

C#图片按比例缩放

C#图片按比例缩放

工具类代码:

using System;
 System.Collections.Generic;
 System.Drawing;
 System.Drawing.drawing2d;
 System.Drawing.Imaging;
 System.IO;
 System.Linq;
 System.Text;
 System.Threading.Tasks;

namespace ZoomImage.Utils
{
    /// <summary>
    /// 图片缩放
    </summary>
    public class ZoomImageUtil
    {
        #region 图片缩放
        <summary>
         图片缩放
        </summary>
        <param name="bArr">图片字节流</param>
        <param name="width">目标宽度,若为0,表示宽度按比例缩放<param name="height">目标长度,若为0,表示长度按比例缩放</param>
        static byte[] GetThumbnail(byte[] bArr,int width,1)">int height)
        {
            if (bArr == null) return null;
            MemoryStream ms = new MemoryStream(bArr);
            Bitmap bmp = (Bitmap)Image.FromStream(ms);
            ms.Close();

            bmp = GetThumbnail(bmp,width,height);

            ImageCodecInfo imageCodecInfo = GetEncoder(ImageFormat.Jpeg);
            EncoderParameters encoderParameters = new EncoderParameters(1);
            EncoderParameter encoderParameter = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality,75L);
            encoderParameters.Param[0] = encoderParameter;

            ms =  MemoryStream();
            bmp.Save(ms,imageCodecInfo,encoderParameters);
            byte[] result = ms.ToArray();
            ms.Close();
            bmp.dispose();

            return result;
        }
        #endregion

        <param name="bmp">图片private static Bitmap GetThumbnail(Bitmap bmp,1)">if (width == 0 && height == 0)
            {
                width = bmp.Width;
                height = bmp.Height;
            }
            else
            {
                )
                {
                    width = height * bmp.Width / bmp.Height;
                }
                if (height == )
                {
                    height = width * bmp.Height / bmp.Width;
                }
            }

            Image imgSource = bmp;
            Bitmap outBmp =  Bitmap(width,height);
            Graphics g = Graphics.FromImage(outBmp);
            g.Clear(Color.Transparent);
            // 设置画布的描绘质量     
            g.CompositingQuality = CompositingQuality.Default;
            g.SmoothingMode = SmoothingMode.Default;
            g.InterpolationMode = InterpolationMode.Default;
            g.DrawImage(imgSource,new Rectangle(0,height + 1),1)">,imgSource.Width,imgSource.Height,GraphicsUnit.Pixel);
            g.dispose();
            imgSource.dispose();
            bmp.dispose();
             outBmp;
        }
        #region 椭圆形缩放
         椭圆形缩放
        byte[] GetEllipseThumbnail( (Bitmap)Image.FromStream(ms);

            Bitmap newBmp = using (Graphics g = Graphics.FromImage(newBmp))
            {
                using (TextureBrush br =  TextureBrush(bmp))
                {
                    br.ScaleTransform(width / (float)bmp.Width,height / (float)bmp.Height);
                    g.SmoothingMode = System.Drawing.drawing2d.SmoothingMode.AntiAlias;
                    g.FillEllipse(br,1)">new Rectangle(Point.Empty,1)"> Size(width,height)));
                }
            }
            MemoryStream newMs =  MemoryStream();
            newBmp.Save(newMs,System.Drawing.Imaging.ImageFormat.Png);
             newMs.ToArray();

            bmp.dispose();
            newBmp.dispose();
            ms.Close();
            newMs.dispose();

            #region GetEncoder
        static ImageCodecInfo GetEncoder(ImageFormat format)
        {
            ImageCodecInfo[] codecs = ImageCodecInfo.Getimagedecoders();
            foreach (ImageCodecInfo codec in codecs)
            {
                if (codec.FormatID == format.Guid)
                {
                     codec;
                }
            }
            ;
        }
        #endregion

    }
}
View Code

使用示例:

 System.ComponentModel;
 System.Data;
 System.Threading;
 System.Threading.Tasks;
 System.Windows.Forms;
 ZoomImage.Utils;

 ZoomImage
{
    partial  Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        void Form1_Load(object sender,EventArgs e)
        {
            openFileDialog1.Multiselect = true;
        }

        void txtWidth_KeyPress(if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar))
            {
                e.Handled = ;
            }
        }

        void txtHeight_KeyPress(void btnSelectimage_Click(tryif (txtWidth.Text == "" && txtHeight.Text == "")
                {
                    MessageBox.Show("请输入宽度或高度!");
                    ;
                }

                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    Task.Factory.StartNew(() =>
                    {
                        
                        {
                            string path = Path.GetDirectoryName(openFileDialog1.FileNames[0]) + \\NewImage\\;
                            if (!Directory.Exists(path))
                            {
                                Directory.CreateDirectory(path);
                            }
                            foreach (string file  Directory.GetFiles(path))
                            {
                                File.Delete(file);
                            }

                            int i = string fileName  openFileDialog1.FileNames)
                            {
                                Bitmap bmp = ZoomImageUtil.GetThumbnail(new Bitmap(fileName),Convert.ToInt32(txtWidth.Text == "" ? 0" : txtWidth.Text),Convert.ToInt32(txtHeight.Text ==  : txtHeight.Text));

                                this.Invoke(new InvokeDelegate(() =>
                                {
                                    if (cbxExt.SelectedItem == )
                                    {
                                        bmp.Save(path + Path.GetFileName(fileName));
                                    }
                                    
                                    {
                                        bmp.Save(path + Path.GetFileNameWithoutExtension(fileName) + (string)cbxExt.SelectedItem);
                                    }
                                }));
                                
                                {
                                    lblProgress.Text = string.Format(进度:{1}/{0}",openFileDialog1.FileNames.Length,++i);
                                }));
                                Thread.Sleep();
                            }

                            MessageBox.Show(成功!);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    });
                }
            }
             (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

    }

     跨线程访问控件的委托
    delegate void InvokeDelegate();
}
View Code

 

C#图片按比例缩放实例

C#图片按比例缩放实例

本文实例为大家分享了C#图片按比例缩放的具体代码,供大家参考,具体内容如下

工具类代码:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.drawing2d;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZoomImage.Utils
{
 /// <summary>
 /// 图片缩放
 /// </summary>
 public class ZoomImageUtil
 {
  /// <summary>
  /// 图片缩放
  /// </summary>
  /// <param name="bmp">图片</param>
  /// <param name="width">目标宽度,若为0,表示宽度按比例缩放</param>
  /// <param name="height">目标长度,若为0,表示长度按比例缩放</param>
  public static Bitmap GetThumbnail(Bitmap bmp,int width,int height)
  {
   if (width == 0)
   {
    width = height * bmp.Width / bmp.Height;
   }
   if (height == 0)
   {
    height = width * bmp.Height / bmp.Width;
   }

   Image imgSource = bmp;
   Bitmap outBmp = new Bitmap(width,height);
   Graphics g = Graphics.FromImage(outBmp);
   g.Clear(Color.Transparent);
   // 设置画布的描绘质量   
   g.CompositingQuality = CompositingQuality.HighQuality;
   g.SmoothingMode = SmoothingMode.HighQuality;
   g.InterpolationMode = InterpolationMode.HighQualityBicubic;
   g.DrawImage(imgSource,new Rectangle(0,width,height + 1),imgSource.Width,imgSource.Height,GraphicsUnit.Pixel);
   g.dispose();
   imgSource.dispose();
   bmp.dispose();
   return outBmp;
  }
 }
}

使用示例:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZoomImage.Utils;

namespace ZoomImage
{
 public partial class Form1 : Form
 {
  public Form1()
  {
   InitializeComponent();
  }

  private void Form1_Load(object sender,EventArgs e)
  {
   openFileDialog1.Multiselect = true;
  }

  private void txtWidth_KeyPress(object sender,KeyPressEventArgs e)
  {
   if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar))
   {
    e.Handled = true;
   }
  }

  private void txtHeight_KeyPress(object sender,KeyPressEventArgs e)
  {
   if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar))
   {
    e.Handled = true;
   }
  }

  private void btnSelectimage_Click(object sender,EventArgs e)
  {
   try
   {
    if (txtWidth.Text == "" && txtHeight.Text == "")
    {
     return;
    }

    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
     Task.Factory.StartNew(() =>
     {
      string path = Path.GetDirectoryName(openFileDialog1.FileNames[0]) + "\\NewImage\\";

      int i = 0;
      foreach (string fileName in openFileDialog1.FileNames)
      {
       Bitmap bmp = ZoomImageUtil.GetThumbnail(new Bitmap(fileName),Convert.ToInt32(txtWidth.Text == "" ? "0" : txtWidth.Text),Convert.ToInt32(txtHeight.Text == "" ? "0" : txtHeight.Text));
       if (!Directory.Exists(path))
       {
        Directory.CreateDirectory(path);
       }
       File.Delete(path + Path.GetFileName(fileName));
       bmp.Save(path + Path.GetFileName(fileName));
       this.Invoke(new InvokeDelegate(() =>
       {
        lblProgress.Text = string.Format("进度:{1}/{0}",openFileDialog1.FileNames.Length,++i);
       }));
       Thread.Sleep(1);
      }

      MessageBox.Show("成功!");
     });
    }
   }
   catch (Exception ex)
   {
    MessageBox.Show(ex.Message);
   }
  }

 }

 /// <summary>
 /// 跨线程访问控件的委托
 /// </summary>
 public delegate void InvokeDelegate();
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

C#图片按比例缩放的示例代码分享

C#图片按比例缩放的示例代码分享

这篇文章主要为大家详细介绍了c#图片按比例缩放的实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C#图片按比例缩放的具体代码,供大家参考,具体内容如下

工具类代码:


using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZoomImage.Utils
{
 /// <summary>
 /// 图片缩放
 /// </summary>
 public class ZoomImageUtil
 {
  /// <summary>
  /// 图片缩放
  /// </summary>
  /// <param name="bmp">图片</param>
  /// <param name="width">目标宽度,若为0,表示宽度按比例缩放</param>
  /// <param name="height">目标长度,若为0,表示长度按比例缩放</param>
  public static Bitmap GetThumbnail(Bitmap bmp, int width, int height)
  {
   if (width == 0)
   {
    width = height * bmp.Width / bmp.Height;
   }
   if (height == 0)
   {
    height = width * bmp.Height / bmp.Width;
   }

   Image imgSource = bmp;
   Bitmap outBmp = new Bitmap(width, height);
   Graphics g = Graphics.FromImage(outBmp);
   g.Clear(Color.Transparent);
   // 设置画布的描绘质量   
   g.CompositingQuality = CompositingQuality.HighQuality;
   g.SmoothingMode = SmoothingMode.HighQuality;
   g.InterpolationMode = InterpolationMode.HighQualityBicubic;
   g.DrawImage(imgSource, new Rectangle(0, 0, width, height + 1), 0, 0, imgSource.Width, imgSource.Height, GraphicsUnit.Pixel);
   g.Dispose();
   imgSource.Dispose();
   bmp.Dispose();
   return outBmp;
  }
 }
}
登录后复制

使用示例:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZoomImage.Utils;

namespace ZoomImage
{
 public partial class Form1 : Form
 {
  public Form1()
  {
   InitializeComponent();
  }

  private void Form1_Load(object sender, EventArgs e)
  {
   openFileDialog1.Multiselect = true;
  }

  private void txtWidth_KeyPress(object sender, KeyPressEventArgs e)
  {
   if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar))
   {
    e.Handled = true;
   }
  }

  private void txtHeight_KeyPress(object sender, KeyPressEventArgs e)
  {
   if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar))
   {
    e.Handled = true;
   }
  }

  private void btnSelectImage_Click(object sender, EventArgs e)
  {
   try
   {
    if (txtWidth.Text == "" && txtHeight.Text == "")
    {
     return;
    }

    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
     Task.Factory.StartNew(() =>
     {
      string path = Path.GetDirectoryName(openFileDialog1.FileNames[0]) + "\\NewImage\\";

      int i = 0;
      foreach (string fileName in openFileDialog1.FileNames)
      {
       Bitmap bmp = ZoomImageUtil.GetThumbnail(new Bitmap(fileName), Convert.ToInt32(txtWidth.Text == "" ? "0" : txtWidth.Text), 
       Convert.ToInt32(txtHeight.Text == "" ? "0" : txtHeight.Text));
       if (!Directory.Exists(path))
       {
        Directory.CreateDirectory(path);
       }
       File.Delete(path + Path.GetFileName(fileName));
       bmp.Save(path + Path.GetFileName(fileName));
       this.Invoke(new InvokeDelegate(() =>
       {
        lblProgress.Text = string.Format("进度:{1}/{0}", openFileDialog1.FileNames.Length, ++i);
       }));
       Thread.Sleep(1);
      }

      MessageBox.Show("成功!");
     });
    }
   }
   catch (Exception ex)
   {
    MessageBox.Show(ex.Message);
   }
  }

 }

 /// <summary>
 /// 跨线程访问控件的委托
 /// </summary>
 public delegate void InvokeDelegate();
}
登录后复制

以上就是C#图片按比例缩放的示例代码分享的详细内容,更多请关注php中文网其它相关文章!

关于php实现图片按比例截取的方法php实现图片按比例截取的方法是的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于.Net实现上传图片按比例自动缩小或放大的方法、C#图片按比例缩放、C#图片按比例缩放实例、C#图片按比例缩放的示例代码分享等相关知识的信息别忘了在本站进行查找喔。

本文标签:

上一篇PHP常用排序算法实例小结【基本排序,冒泡排序,快速排序,插入排序】

下一篇php实现的中文分词类完整实例(php实现的中文分词类完整实例是什么)