2010-06-21 5 views
2

TIFF 이미지의 dpi 값을 코드를 통해 C#으로 설정하려고하지만 이미지 저장 후 값이 유지되지 않습니다.DPI 값을 Tiff로 설정 C#

using (var image = new Bitmap(@"c:\newimage.tif")) 
{ 
    uint[] uintArray = { 300, 1}; //Setting DPI as 300 
    byte[] bothArray = ConvertUintArrayToByteArray(uintArray); 
    PropertyItem item = image.PropertyItems.Where(p => p.Id == 0x11A).Single(); 
    var val = BitConverter.ToUInt32(item.Value, 0); 
    Console.WriteLine(val); 
    item.Id = 0x11A; 
    item.Value = bothArray; 
    item.Type = 5; 
    item.Len = item.Value.Length; 
    image.SetPropertyItem(item); 
    image.Save(@"c:\newimage1.tif"); //Save image to new File 
} 

이 코드의 잘못된 점은 무엇입니까? 모든 종류의 도움을 주시면 감사하겠습니다. TIFF file tag definitions

답변

3

비트 맵 해상도가 속성보다 우선합니다. Bitmap.SetResolution()을 사용하십시오.

+0

나는'SetResolution를 (사용하여 시도)'하지만 여전히 기쁨 :( –

-1

면책 조항 : 저는 Atalasoft으로 근무하며 .NET을 통해 이미지 작업용 제품을 만듭니다.

당신이이 같은 코드로 TiffDocument 클래스를 사용 dotImage입니다 수행 할 수 있습니다

private void RemoveIfContains(TiffTagCollection tags, int tagID) 
{ 
    TiffTag tag = tags.LookupTag(tagID); 
    if (tag != null) 
     tags.Remove(tag); 
} 

public void SetTiffResolution(string tiffin, string tiffout, int page, double resolution) { 
    if (tiffin == tiffout) 
     throw new ArgumentException(tiffout, "output path must be different from input path"); 
    TiffFile tf = new TiffFile(); 
    using (FileStream stm = new FileStream(tiffin, FileMode.Open, FileAccess.Read, FileShare.Read)) { 
     tf.Read(stm); 
     TiffDirectory image = tf.Images[page]; 
     RemoveIfContains(image.Tags, TiffTagID.ResolutionX); 
     RemoveIfContains(image.Tags, TiffTagID.ResolutionY); 
     RemoveIfContains(image.Tags, TiffTagID.ResolutionUnit); 
     image.Tags.Add(new TiffTag(TiffTagID.ResolutionX, resolution); 
     image.Tags.Add(new TiffTag(TiffTagID.ResolutionY, resolution); 
     image.Tags.Add(new TiffTag(TiffTagID.ResolutionUnit, 2)); // 2 == dots per INCH 
     tf.Save(tiffout); 
    } 
}   

장점은 여러 가지입니다 - 태그에 직접 접근이 이미지를 재 인코딩없이 일 당신이 원하는 방식을 설정할 수 있습니다 . 초기 이미지가 TIFF 내에서 JPEG로 압축 된 경우 JPEG 데이터를 다시 인코딩하고 정보를 잃어 버릴 염려가 없습니다. 또한 이미 있던 TIFF 태그 정보를 잃어 버릴 염려가 없습니다. 이 코드는 다중 페이지 TIFF에서도 작동합니다. 마지막으로 태그를 변경하기 위해 전체 이미지를 디코딩하는 것보다 빠른 접근 방법 일 것입니다.

이 접근법의 단점은 정말, 정말, TIFF 사양을 이해해야한다는 것입니다. 이러한 이해가 없으면 항상 파싱 할 수없는 파일을 생성 할 위험이 있습니다. 예를 들어 DotImage와 IrfanView는 모든 방식의 깨진 TIFF를 사용하는 데 매우 관대합니다. Adobe PhotoShop은 악명 높게 엄격합니다.

+4

내가 아픈 받고 있어요 : 여기

는 .NET을 사용하여 읽기와 X를 작성하고 Y의 해결 방법 TIFF 이미지의 예 StackOverflow (귀하와 Lou Franco 모두)의 게시물을 응답하여 뻔뻔스런 광고를 보냅니다. 여기에 광고를해야한다면 실제 광고를 구매하십시오. –

5

속성 값과 비트 맵 해상도를 모두 설정 한 다음 이미지를 다시 저장하면 해상도가 변경됩니다 (예제 이미지에서 효과가 있음). 원본 파일에는 X 및 Y 해상도의 태그가 있어야하며 .NET에 해당 태그가없는 경우 추가해야하는지 확실하지 않습니다 (테스트해야 함).

int numerator, denominator; 

using (Bitmap bmp = (Bitmap)Bitmap.FromFile(@"C:\input.tif")) 
{ 
    // obtain the XResolution and YResolution TIFFTAG values 
    PropertyItem piXRes = bmp.GetPropertyItem(282); 
    PropertyItem piYRes = bmp.GetPropertyItem(283); 

    // values are stored as a rational number - numerator/denominator pair 
    numerator = BitConverter.ToInt32(piXRes.Value, 0); 
    denominator = BitConverter.ToInt32(piXRes.Value, 4); 
    float xRes = numerator/denominator; 

    numerator = BitConverter.ToInt32(piYRes.Value, 0); 
    denominator = BitConverter.ToInt32(piYRes.Value, 4); 
    float yRes = numerator/denominator; 

    // now set the values 
    byte[] numeratorBytes = new byte[4]; 
    byte[] denominatorBytes = new byte[4]; 

    numeratorBytes = BitConverter.GetBytes(600); // specify resolution in numerator 
    denominatorBytes = BitConverter.GetBytes(1); 

    Array.Copy(numeratorBytes, 0, piXRes.Value, 0, 4); // set the XResolution value 
    Array.Copy(denominatorBytes, 0, piXRes.Value, 4, 4); 

    Array.Copy(numeratorBytes, 0, piYRes.Value, 0, 4); // set the YResolution value 
    Array.Copy(denominatorBytes, 0, piYRes.Value, 4, 4); 

    bmp.SetPropertyItem(piXRes); // finally set the image property resolution 
    bmp.SetPropertyItem(piYRes); 

    bmp.SetResolution(600, 600); // now set the bitmap resolution 

    bmp.Save(@"C:\output.tif"); // save the image 
}