c#


Bitmap.SetResolution creates unprecise values when saved


I'm working on a program to check all PNG images in a directory for correct resolutions and reset the incorrect ones. For every incorrect file I use the code below to reset its resolution.
float ppi = 96.0F;
string absolutePath = "...";
[...]
FileStream fs = new FileStream(absolutePath, FileMode.Open);
Image image = Image.FromStream(fs);
fs.Close();
if (image != null)
{
Bitmap bitmap = new Bitmap(image);
bitmap.SetResolution(ppi, ppi);
bitmap.Save(absolutePath, System.Drawing.Imaging.ImageFormat.Png);
}
When I read the resolution infos from the bitmap variable (bitmap.HorizontalResolution, bitmap.VerticalResolution) after saving they are printed out correct (96).
But when I restart the program the same files are displayed as wrong. Now all resolutions are set to 95,9866. Before there were many different values.
I already tried to cast the ppi for SetResolution as integer, without success.
Did I make any mistakes or is this a problem with the file format? Is there a better way to do this?
As Hans Passant pointed out the resolution can not be set exactly due to the conversion between imperial and metrical units in the file format. So I found a way to remove it entirely:
Ken Silverman's tool PNGOUT.exe is used to optimize PNG images and thereby removes all explicit DPI information. Once removed, WPF assumes the image's resolution as it's default 96 DPI resolution. If the image is used with it's original measures no scaling is needed - which might also improve the performance of the UI. But the performance impact is still to be tested.

Related Links

Html Table with button per row doesn't pass data to the controller
Create write and save xml file dynamically in c# .net
A better way than repeated exception filter?
Form won't sent collection to controller if collection contains two elements
Disable alt key temporary c#
JSON deserialization not deserializing?
index an array to put correct string values with numeric values
What is the use of a public constructor in a private class
How to modify angle of Unity's transform.forward
Obtain Request Token From REST API using Oauth1 in C#
C# Console App Executed by SQL Agent
How to group LINQ to Entities results by enum and date?
StructureMap registering generic types for using them in injected array collection
How to open or pick multiple images in windows store with PickMultipleFilesAsync() using c#?
ASP.NET Core Web API Throws HTTP 500
How do I change a cell font in google sheets api v4 in C#?

Categories

HOME
autohotkey
redux
sql-server-2014
abc
python-requests
jquery-plugins
stdout
okhttp3
salesforce-lightning
mediarecorder
browser-cache
liquidsoap
collision-detection
sitemap
google-api-oauth
unreal-engine4
adroitlogic
guidewire
comma
android-notifications
dat.gui
orange
cherrypy
polybase
drombler-fx
angular-schema-form
pushpad
akamai
oracle-nosql
flickr
sampling
ponylang
lex
instances
lfe
socketcluster
redbean
binary-decision-diagram
colorbox
pexpect
scrapinghub
flask-socketio
extjs3
audio-fingerprinting
sharpdevelop
selection-sort
dmalloc
pyautogui
lightspeed
scanf
timefield
easy-thumbnails
mavlink
git-push
class-design
geokit
jigsaw
particle-system
magick.net
cherry-pick
instabug
dday
clear
github-for-mac
matlab-engine
bioconductor
adempiere
cyberduck
page-refresh
log-analysis
copy-on-write
indices
crystal-reports-7
worklight-appcenter
dateadd
webshim
image-preloader
iwork
imaplib
caroufredsel
playing-cards
junction-table
gmail-imap
gssapi
ios6-maps
optimistic-concurrency
motorola-droid
maven-eclipse-plugin
zend-validate
mcpd
pex-and-moles
recess
lightopenid
auto-compile
fxcopcmd
xcdatamodel
datarelation
vista64
high-load

Resources

Encrypt Message