Excel行高与像素值,列宽与像素值之间的转换,绘制Excel像素画。

本文介绍了一种将像素转换为不同格式的方法,并通过具体的代码示例展示了如何计算基于像素数量的宽度变化。此外,还提供了一个计算像素到英寸转换系数(DPI)的函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

private static float GetDpiX()
{
    try
    {
        using (Bitmap image = new Bitmap(1, 1))
        {
            using (Graphics graphics = Graphics.FromImage(image))
            {
                return graphics.DpiX;
            }
        }
    }
    catch
    {
        return 72;
    }
}

传入像素numPixel

int numPixel = (int)numericUpDown1.Value;
Bitmap image = ConvertToFormat(fileImage.Image, PixelFormat.Format24bppRgb);
//Bitmap image = (Bitmap)fileImage.Image;
g = pictureBox1.CreateGraphics();


double rowHeight = (double)((float)numPixel * 72f / (float)GetDpiX());
                 
for (int i = 0; i < 100; i++)
{


    double colWidth = Math.Round((numPixel < 12 ? (numPixel / 12.00) : (1 + ((numPixel - 12) / 7.00))), 2);
    int _numPixel = (int)Math.Round(colWidth < 1 ? colWidth * 12.0 : 12 + (colWidth - 1) * 7.00); 
    Console.WriteLine(string.Format($"{numPixel}  {colWidth}   {_numPixel}"));
    numPixel = numPixel + 1;
    //double width = (double)((float)numPixel * 72f / (float)GetDpiX());
}

 打印出100个与excel里面对比

 

10.08
20.17
30.25
40.33
50.42
60.5
70.58
80.67
90.75
100.83
110.92
121
131.14
141.29
151.43
161.57
171.71
181.86
192
202.14
212.29
222.43
232.57
242.71
252.86
263
273.14
283.29
293.43
303.57
313.71
323.86
334
344.14
354.29
364.43
374.57
384.71
394.86
405
415.14
425.29
435.43
445.57
455.71
465.86
476
486.14
496.29
506.43
516.57
526.71
536.86
547
557.14
567.29
577.43
587.57
597.71
607.86
618
628.14
638.29
648.43
658.57
668.71
678.86
689
699.14
709.29
719.43
729.57
739.71
749.86
7510
7610.14
7710.29
7810.43
7910.57
8010.71
8110.86
8211
8311.14
8411.29
8511.43
8611.57
8711.71
8811.86
8912
9012.14
9112.29
9212.43
9312.57
9412.71
9512.86
9613
9713.14
9813.29
9913.43
10013.57
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

功克

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值