• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
初出茅庐
我的技术之路....每天前进一小步
博客园    首页    新随笔    联系   管理    订阅  订阅

按原图比例缩放,居中,多余的裁剪,图像之间留间距

/// <summary>
       
/// 按原图比例缩放,居中,多余的裁剪,图像之间留间距
       
/// </summary>
       
/// <param name="sourceFile"></param>
       
/// <param name="destFilePath"></param>
       
/// <param name="destWidth"></param>
       
/// <param name="destHeight"></param>
        public static void ImageCutZoomSize(System.IO.Stream sourceFile, string destFilePath, int destWidth, int destHeight)
        {
           
try
            {
               
//源图片
                System.Drawing.Image bmp = System.Drawing.Bitmap.FromStream(sourceFile);
                System.Drawing.Imaging.ImageFormat thisFormat
= bmp.RawFormat;

               
int w = bmp.Width;
               
int h = bmp.Height;

               
//int x = 0;
               
//int y = 300;

               
//图像之间留2个像素间距 y += 1;
               
//destHeight -= 2;
               
//计算图片宽高比
                double ratio_img = (double)w / h;
               
double ratio_blk = (double)destWidth / destHeight; //0.44270833

               
int img_x = 0, img_y = 1;
               
int cut_w = w, cut_h = h;

               
if (ratio_img >= ratio_blk)
                {
                   
//宽图片,求img_x
                    cut_w = (int)Math.Round(h * ratio_blk);
                    img_x
+= (w - cut_w) / 2;
                }
               
else
                {
                   
//高图片,求img_y
                    cut_h = (int)Math.Round(w / ratio_blk);
                    img_y
+= (h - cut_h) / 2;
                }

               
//裁剪后新的值
               
               
//System.Windows.Forms.MessageBox.Show("img_x" + img_x + "," +
               
//    "img_y" + img_y + "," +
               
//    "cut_w" + cut_w + "," +
               
//    "cut_h" + cut_h + ",裁剪后新的值");
                
               
if (img_x + cut_w > w)
                {
                    cut_w
= w - img_x;
                }

               
if (img_y + cut_h > h)
                {
                    cut_h
= h - img_y;
                }

               
//裁剪
                Image bmpOut = new Bitmap(cut_w, cut_h, PixelFormat.Format24bppRgb);

                Graphics gcut
= Graphics.FromImage(bmpOut);
                gcut.Clear(Color.Black);
                gcut.DrawImage(bmp,
new Rectangle(0, 0, cut_w, cut_h), new Rectangle(img_x, img_y, cut_w, cut_h), GraphicsUnit.Pixel);
                gcut.Dispose();
               
//bmpOut.Save(@"E:\19gcut.jpg");
               
//裁剪 完成 
                ////缩放
                //目标图片
                Image destImg = new Bitmap(destWidth, destHeight);
                Graphics gresize
= Graphics.FromImage(destImg);
                gresize.Clear(Color.Black);
               
// 插值算法的质量 设置画布的描绘质量
                gresize.CompositingQuality = CompositingQuality.HighQuality;
                gresize.SmoothingMode
= SmoothingMode.HighQuality;
                gresize.InterpolationMode
= InterpolationMode.HighQualityBicubic;

                gresize.DrawImage(bmpOut,
new Rectangle(0, 0, destWidth, destHeight), new Rectangle(0, 0, bmpOut.Width, bmpOut.Height), GraphicsUnit.Pixel);
                gresize.Dispose();
               
//destImg.Save(@"E:\19resizet.jpg");//ok
                ////缩放完成


               
// 以下代码为保存图片时,设置压缩质量
                EncoderParameters encoderParams = new EncoderParameters();
               
long[] quality = new long[1];
                quality[
0] = 100;

                EncoderParameter encoderParam
= new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
                encoderParams.Param[
0] = encoderParam;


               
//获得包含有关内置图像编码解码器的信息的ImageCodecInfo 对象。
                ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
                ImageCodecInfo jpegICI
= null;
               
for (int a = 0; a < arrayICI.Length; a++)
                {
                   
if (arrayICI[a].FormatDescription.Equals("JPEG"))
                    {
                        jpegICI
= arrayICI[a];//设置JPEG编码
                        break;
                    }
                }

               
if (jpegICI != null)
                {
                    destImg.Save(destFilePath, jpegICI, encoderParams);
                }
               
else
                {
                    destImg.Save(destFilePath, thisFormat);
                }
               
/*****************/


                bmp.Dispose();
                bmpOut.Dispose();
                destImg.Dispose();
               
if(sourceFile.ReadByte()<0)
                    sourceFile.Close();

            }
           
catch (Exception ex)
            {
               
throw ex;
            }
           
//finally
           
//{
           
//    bmp.Dispose();
           
//    bitmap.Dispose();
           
//}
        }
        


     
       
/// <summary>
       
/// 返回本机的IPAddress[],一般第一个就是本机网卡的IP
       
/// </summary>
        public static System.Net.IPAddress[] LocalHostIPAddress
        {
           
get
            {
               
//Dns.GetHostByName(Dns.GetHostName()).;
                return System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName()).AddressList;
            }
        }
        

     
       
      
       
/// <summary>
       
/// 判断是否合法IP   /// </summary>
       
/// <param name="strIP"></param>
       
/// <returns></returns>
        public static bool ValidateIPAddress(string strIP)
        {
           
//if (null == strIP || "" == strIP.Trim() || Convert.IsDBNull(strIP))
            if (string.IsNullOrEmpty(strIP.Trim()) || Convert.IsDBNull(strIP))
            {
return false; }

           
return System.Text.RegularExpressions.Regex.IsMatch(strIP, @"^((\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.){3}(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$");
        }

       
public static DirectoryInfo MaxDirRoot()
        {
            List
<DriveInfo> dirlist = new List<DriveInfo>();

           
foreach (DriveInfo dri in DriveInfo.GetDrives())
            {
               
if (dri.DriveType == DriveType.Fixed)//确定固定硬盘
                {
                    dirlist.Add(dri);
                }
            }
           
if (dirlist.Count > 1)
            {
                DriveInfo drinfo
= null;
               
for (int d = 1; d < dirlist.Count; d++)
                {
                   
if (dirlist[d - 1].TotalFreeSpace < dirlist[d].TotalFreeSpace)
                    {
                        drinfo
= dirlist[0];
                        dirlist[
0] = dirlist[d + 1];
                        dirlist[d]
= drinfo;
                    }
                }
               
//MessageBox.Show(drinfo.Name + drinfo.TotalFreeSpace.ToString());
                DirectoryInfo dirinfo = drinfo.RootDirectory.CreateSubdirectory("mediadatas");
                dirinfo.Attributes
= FileAttributes.Directory;
                dirinfo.Attributes
= FileAttributes.Hidden;
               
return dirinfo;
            }
           
else
            {

                DirectoryInfo dirinfo
= dirlist[0].RootDirectory;
               
return dirinfo;
            }
        }

    }
}
posted @ 2008-07-30 11:34  Cheek G  阅读(989)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3