zoukankan      html  css  js  c++  java
  • 【C#】取得并改变图像解析度

    using System;
    using System.Drawing;
    using System.IO;

    class Program
    {
      static void Main()
      {
        // 画像を読み込む
        string basePath = @"C:picture.png";
        Bitmap bmpOrg = Bitmap.FromFile(basePath) as Bitmap;

        // 画像解像度を取得する
        float hRes = bmpOrg.HorizontalResolution;
        float vRes = bmpOrg.VerticalResolution;
        Console.WriteLine(
          "水平解像度:{0} dpi、垂直解像度:{1} dpi", hRes, vRes);

        if (hRes != 96.0F || vRes != 96.0F)
        {
          // 画像解像度を変更して新しいBitmapオブジェクトを作成
          Bitmap bmpNew = new Bitmap(bmpOrg.Width, bmpOrg.Height);
          bmpNew.SetResolution(96.0F96.0F);

          // 新しいBitmapオブジェクトに元の画像内容を描画
          Graphics g = Graphics.FromImage(bmpNew);
          g.DrawImage(bmpOrg, 00, bmpOrg.Width, bmpOrg.Height);
          g.Dispose();

          // 画像を保存
          string dirName = Path.GetDirectoryName(basePath);
          string fileName = Path.GetFileNameWithoutExtension(basePath);
          string extName = Path.GetExtension(basePath);
          string newPath = Path.Combine(
            dirName, fileName + "_new" + extName);
          bmpNew.Save(newPath);
          bmpNew.Dispose();
          Console.WriteLine("解像度を96dpiに変更しました。");
        }

        // 画像リソースを解放
        bmpOrg.Dispose();

        // メッセージを確認できるように実行を停止
        Console.ReadKey();
      }
    }
  • 相关阅读:
    mysql存儲過程+游標的應用實例5/17
    mysql存儲過程+遊標應用之:找缺號5/19
    轉:愚公移山
    c++中的头文件
    栈和堆:生存空间
    java中的类加载
    c++中的连接
    访问static变量和方法
    子类调用构造函数的过程
    c++中变量的存储种类
  • 原文地址:https://www.cnblogs.com/sekihin/p/5415557.html
Copyright © 2011-2022 走看看