用于更改图片dpi [英] For change picture dpi

查看:192
本文介绍了用于更改图片dpi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将图片dpi从96 dpi更改为300 dpi ic#



我尝试了什么:



private void button2_Click(对象发送者,EventArgs e)

{

var filename = @ - 输入 - 图像 - - ;

var targetfile = @ - output-image--;



using(var image = Image.FromFile( filename))

{

//改为96 DPI

var resolution = new byte [8] {0,0,96,0 ,0,0,1,0};



if(image.PropertyIdList.Contains(282))

{

var xResolution = image.GetPropertyItem(300);

xResolution.Value = resolution;

image.SetPropertyItem(xResolution);

}

if(image.PropertyIdList.Contains(283))

{

var yResolution = image.GetPropertyItem(300);

yResolution.Value = resolution;

image.SetPropertyItem(yResolution);

}



image.Save(targetfile,image.RawFormat);

}

}

但这不起作用

how can i change picture dpi from 96 dpi to 300 dpi i c#

What I have tried:

private void button2_Click(object sender, EventArgs e)
{
var filename = @"--input--image--";
var targetfile = @"--output-image--";

using (var image = Image.FromFile(filename))
{
// changing to 96 DPI
var resolution = new byte[8] { 0, 0, 96, 0, 0, 0, 1, 0 };

if (image.PropertyIdList.Contains(282))
{
var xResolution = image.GetPropertyItem(300);
xResolution.Value = resolution;
image.SetPropertyItem(xResolution);
}
if (image.PropertyIdList.Contains(283))
{
var yResolution = image.GetPropertyItem(300);
yResolution.Value = resolution;
image.SetPropertyItem(yResolution);
}

image.Save(targetfile, image.RawFormat);
}
}
but this is not working

推荐答案

更改分辨率很复杂:它不仅仅是改变一个简单变量的情况。

您需要查看图像的存储方式:它们包含固定数量的像素(x宽度x高度),DPI值仅控制最初在特定设备上以物理英寸显示的像素数量当图像被绘制到它。更改文件中的DPI值不会改变像素数,并且通常不会影响图像的显示方式,因为它经常被拉伸或收缩以适合显示器或页面的特定部分。



通过重新渲染图像可以升级或缩小分辨率,但这不会增加细节(尽管你在电视上看到的东西) - 它只是在升级时使像素更大或外推额外的像素值,或在缩小时丢弃细节。即便如此,这也不是一个简单的过程。



读取和保存文件是一个坏主意,除非你真的,真的需要 - 大多数图像都是压缩​​的,而且最多流行格式 - jpeg - 使用有损压缩,因此每次保存图像时,都会丢失无法恢复的数据。通常4或5次保存足以将文件减少到不可用性,但即使是一次额外的操作也可以明显地软化细节。
Changing resolution is complicated: it's not just a case of altering a simple variable.
You need to look at how images are stored: they contain a fixed number of pixels (x width by y height) and the DPI value only controls how many of those are initially displayed in a physical inch on a particular device when the image is drawn to it. Changing the DPI values in the file doesn't change the number of pixels, and often won't affect how the image is displayed, given that it's frequently stretched or shrunk to fit into a particular part of a display or page.

It's possible to "upscale" or "downscale" the resolution by re-rendering the image, but that doesn't add detail (despite what you see on TV) - it just makes pixels bigger or extrapolates extra pixel values when you upscale, or discards details when you downscale. Even then, it's not a trivial process.

And reading and saving files is a bad idea unless you really, really need to - most images are compressed, and the most popular format - jpeg - uses a "lossy" compression, so each time you save the image, you lose data which you cannot recover. Often 4 or 5 saves is enough to reduce a file to unusability, but even one extra can "soften" detail out noticeably.


这篇关于用于更改图片dpi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆