参数无效。 [英] Parameter is not valid.

查看:138
本文介绍了参数无效。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写的这段代码:

this code i had written:

private Bitmap CreateBarcode(string s)
       {
           Bitmap barcode = new Bitmap(1, 1);
           Font font = new Font("Free Font", 60, FontStyle.Regular, GraphicsUnit.Point);

           Graphics graphic = Graphics.FromImage(barcode);

           SizeF size = graphic.MeasureString(s, font);

     barcode = new Bitmap(barcode,size.ToSize());// here is error/ here is error;      

           graphic = Graphics.FromImage(barcode);
           graphic.Clear(Color.White);

           graphic.TextRenderingHint = TextRenderingHint.SingleBitPerPixel;
           graphic.DrawString(s, font, new SolidBrush(Color.Black), 0, 0);
           graphic.Flush();
           font.Dispose();
           graphic.Dispose();

           return barcode;

       }





我的尝试:



i更改了size参数但它不起作用



What I have tried:

i had change the size parameter but it does not work

推荐答案

我将你的代码粘贴到我的应用程序中,这是我唯一的方法你可以得到错误的是你传递一个空字符串:

I pasted your code into my app, and the only way I could get teh error you do is you pass it an empty string:
Bitmap b = CreateBarcode("");



哪个会给你一个(0,0)的大小,这会导致错误。



因此检查你的方法参数是否为空,所有空格或为空 - 并返回一个空白位图。


Which would give you a size of (0,0) and that would cause the error.

So check your method argument for null, all whitespace, or empty - and return a blank bitmap.


OriginalGriff是比我快一点..但我有一个关于如何改进你的代码的建议:使用 -blocks使用而不是显式处理对象。它将确保您的对象在离开示波器时无论如何被处理,并且它提高了可读性(嗯,大多数人至少这么认为)。此外,通过将第二个Graphics-object重新分配给您的变量 graphics ,您不会丢弃第一个。

OriginalGriff was a bit faster than me.. but I have a suggestion on how to otherwise improve your code: Instead of explicitly disposing objects, use using-blocks. It will ensure that your object is being disposed "no matter what" when leaving the scope and it improves readibility (well, most people think so at least). Also, by re-assigning a second Graphics-object to your variable graphics you're not disposing the first one.
private Bitmap CreateBarcode(string text)
{
    if (String.IsNullOrEmpty(text))
        throw new ArgumentException(nameof(text) + " may not be null or empty.");

    Bitmap barcode;

    using (Font font = new Font("Free Font", 60, FontStyle.Regular, GraphicsUnit.Point))
    {
        using (Bitmap dummyBitmap = new Bitmap(1, 1))
        using (Graphics graphic = Graphics.FromImage(dummyBitmap))
        {
            SizeF size = graphic.MeasureString(text, font);
            barcode = new Bitmap(dummyBitmap, size.ToSize());
        }

        using (Graphics graphic = Graphics.FromImage(barcode))
        {
            graphic.Clear(Color.White);
            graphic.TextRenderingHint = TextRenderingHint.SingleBitPerPixel;
            graphic.DrawString(text, font, new SolidBrush(Color.Black), 0, 0);
            graphic.Flush();
        }
    }

    return barcode;
}


谢谢你们两位



它通过其他方式解决了但是那里是发生了其他问题,关于GDI +错误



这是



保存路径





Thanks both of you

it's solved by other way but there is other issue occurred which is regarding GDI+ error

which is

at save path


Bitmap barcode = CreateBarcode(txtGenearte.Text);
barcode.Save(@"C:\barcode.gif",ImageFormat.Gif);=>A generic error occurred in GDI+.


这篇关于参数无效。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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