2009-10-23 129 views
3

我有一個要求,要求與10×6,88釐米的圖像。 我知道我不能簡單地從cm轉換爲像素,導致一個像素大小取決於用戶的顯示分辨率。 我想知道是否有一種方法來調整圖像的大小以釐米爲單位。 (我需要保持圖像擴展還如:不能將其轉換爲PDF或其它擴展名)以釐米C調整圖像大小#

+0

可你只是圖像分辨率去? – 2009-10-23 12:35:43

+0

要澄清,圖像大小取決於圖像分辨率。當它顯示在屏幕上時,應該明確縮放到設備分辨率(但通常沒有)。有關詳細信息,請參閱此處:http://www.atalasoft.com/cs/blogs/stevehawley/archive/2006/10/05/10954.aspx – plinth 2009-10-23 12:41:20

回答

4

這真的取決於其分辨率,用戶將打印圖像(以釐米大小几乎沒有什麼意義比其他當打印時)。如果用戶想要打印,例如200 dpi,那麼圖像需要(10/2.54 * 200)(6.88/2.54 * 200)像素(需要2.54的分區才能在釐米和英寸之間轉換)。所需的分辨率高度依賴於它是什麼樣的圖像以及用戶的質量要求。

所以只是說:「我想用Y釐米調整爲X」並沒有真正意義。

關於如何使實際調整大小,一旦你已經想通了圖像的大小需要,this SO answer應包括您需要的代碼示例。

+0

不同意「以釐米爲單位的尺寸除了打印時沒什麼意義」,尺寸以釐米爲單位時,如果您必須查看與將要打印的尺寸相同的文檔(例如,所見即所得設計器),則完美無缺。否則,最終會出現經典的WYSINWYG(你看到的並不是你所得到的),就像在微軟報表設計器(SSRS)中一樣。 – 2016-05-13 09:19:53

0

Fredrik在說什麼: 我會拍攝一個漂亮的DPI,並要求圖像的分辨率或更大(但寬高比相同),並且在導出/打印圖像時,將圖像調整爲DPI使用其他程序/打印機...

0

這可能是如此簡單:大多數圖像存儲在他們每英寸的像素數。找出圖像每個維度的像素數量,並將其除以英寸數(從cm轉換而來)。然後使用原始位,只需修改每英寸像素數的字段(或者更常見的是每英寸點數)。

所以你的圖片需要是3.93「x 2.71」。如果您的圖像是393px x 271px,則可以將dpi設置爲100x100。如果您的圖像是39px x 27px,則可以將dpi設置爲10x10。

雖然可能你必須做一些調整大小,正如其他答案所解釋的那樣。 :)

2

圖像文件格式,如JPG和TIFF具有EXIF header具有像的水平和垂直DPI信息。

因此,如果您獲得具有此元數據的圖像,則可以驗證可打印的尺寸。

double DPC = Image_DPI * 0.393700787; 

double widthInCm = Image_Width * DPC; 
double heightInCm = Image_Height * DPC; 

if (widthInCm <= 10 && heightInCm <= 6.88) // do stuff 

如果您需要調整圖像不能超過這些打印的尺寸,你可以做到這一點的其他方式,並計算DPI比例,讓10cm以內X6.88釐米界尺寸W x H契合的圖像。

2

其實,你在屏幕上圖像的大小來區分,並且圖像尺寸打印輸出。

通常情況下,你會發現公式:

inches = pixels/dpi 

所以如下:

pixel = inches * dpi 

這是打印,其實。
對於顯示器,將DPI替換爲ppi,並且在那裏。

對於那些(如我)不熟悉英寸:

inches = pixels/dpi 
pixel = inches * dpi 
1 centimeter = 0.393700787 inch 
pixel = cm * 0.393700787 * dpi 

這一例程將計算像素尺寸,使顯示器上的圖像顯示的X釐米。
但是在打印機上,您並不那麼容易,因爲您無法像DPI那樣容易地獲得DPI(bmp.Horizo​​ntalResolution & bmp.VerticalResolution)。

public static int Cm2Pixel(double WidthInCm) 
{ 
    double HeightInCm = WidthInCm; 
    return Cm2Pixel(WidthInCm, HeightInCm).Width; 
} // End Function Cm2Pixel 


public static System.Drawing.Size Cm2Pixel(double WidthInCm, double HeightInCm) 
{ 
    float sngWidth = (float)WidthInCm; //cm 
    float sngHeight = (float)HeightInCm; //cm 
    using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(1, 1)) 
    { 
     sngWidth *= 0.393700787f * bmp.HorizontalResolution; // x-Axis pixel 
     sngHeight *= 0.393700787f * bmp.VerticalResolution; // y-Axis pixel 
    } 

    return new System.Drawing.Size((int)sngWidth, (int)sngHeight); 
} // End Function Cm2Pixel 

使用率會是這樣的:

public System.Drawing.Image Generate(string Text, int CodeSize) 
     { 
      int minSize = Cm2Pixel(2.5); // 100; 
      if (CodeSize < minSize) 
       CodeSize = minSize; 

      if (string.IsNullOrEmpty(Text)) 
      { 
       System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(CodeSize, CodeSize); 

       using (System.Drawing.Graphics gfx = System.Drawing.Graphics.FromImage(bmp)) 
       { 

        gfx.Clear(System.Drawing.Color.Black); 
        using(System.Drawing.Font fnt = new System.Drawing.Font("Verdana", 12, System.Drawing.FontStyle.Bold)) 
        { 
         double y = CodeSize/2.0 - fnt.Size; 
         gfx.DrawString("No Data", fnt, System.Drawing.Brushes.White, 5, (int)y, System.Drawing.StringFormat.GenericTypographic); 
        } // End Using fnt 

       } // End using gfx 

       return bmp; 
      } // End if (string.IsNullOrEmpty(Text)) 

...[Generate QR-Code] 
return [Generated QR-Code] 
}