Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中从多行字符串创建自动调整大小的图像(png)#_C#_Asp.net_System.drawing_Drawstring - Fatal编程技术网

C# 在C中从多行字符串创建自动调整大小的图像(png)#

C# 在C中从多行字符串创建自动调整大小的图像(png)#,c#,asp.net,system.drawing,drawstring,C#,Asp.net,System.drawing,Drawstring,我正在寻求从用户输入文本创建一个图像。最终,它将与状态更新一起发送到twitter 我无法控制用户文本的长度-它将是可变的 最初,我能够使用System.Drawing DrawString方法从文本中绘制png,但输出是一个具有潜在巨大宽度的单行png-这显然不是一个可行的解决方案 我有一个想法,将字符串拆分为多个长度,并在png中分别绘制它们,这是可行的,但由于它包裹了单词的中间部分,因此感觉不优雅,而且我不能保证它将与长字符串一起工作(如果25%的sting长度仍然太宽,无法适应png)。

我正在寻求从用户输入文本创建一个图像。最终,它将与状态更新一起发送到twitter

我无法控制用户文本的长度-它将是可变的

最初,我能够使用System.Drawing DrawString方法从文本中绘制png,但输出是一个具有潜在巨大宽度的单行png-这显然不是一个可行的解决方案

我有一个想法,将字符串拆分为多个长度,并在png中分别绘制它们,这是可行的,但由于它包裹了单词的中间部分,因此感觉不优雅,而且我不能保证它将与长字符串一起工作(如果25%的sting长度仍然太宽,无法适应png)。总的来说,它比我最初的解决方案要好,但从长远来看仍然不可行

这是我用来创建png的代码:

string quoteText = someString; //my user's input text
int strLength = quoteText.Length;
        int i25 = strLength / 4;
        string st25 = quoteText.Substring(0, i25);
        string st50 = quoteText.Substring(i25 + 1, i25);
        string st75 = quoteText.Substring((i25 * 2) + 1, i25);
        string st100 = quoteText.Substring((i25 * 3) + 1);


        Bitmap objBmpImage = new Bitmap(500, 600);

        int intWidth = 600;
        int intHeight = 700;

        // Create the Font object for the image text drawing.
        Font objFont = new Font("Roboto Condensed", 14, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel);
        Font brandFont = new Font("IM Fell Double Pica", 14, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Pixel);

        // Create a graphics object to measure the text's width and height.
        Graphics objGraphics = Graphics.FromImage(objBmpImage);

        // This is where the bitmap size is determined.
        intWidth = 500;
        intHeight = 600;


        // Create the bmpImage again with the correct size for the text and font.
        objBmpImage = new Bitmap(objBmpImage, new Size(intWidth, intHeight));

        // Add the colors to the new bitmap.
        objGraphics = Graphics.FromImage(objBmpImage);

        // Set Background color
        objGraphics.Clear(Color.White);
        objGraphics.SmoothingMode = SmoothingMode.AntiAlias;
        objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;
        objGraphics.DrawString(st25, objFont, new SolidBrush(Color.FromArgb(0, 126, 58)), 0, 0);
        objGraphics.DrawString(st50, objFont, new SolidBrush(Color.FromArgb(0, 126, 58)), 0, 30);
        objGraphics.DrawString(st75, objFont, new SolidBrush(Color.FromArgb(0, 126, 58)), 0, 60);
        objGraphics.DrawString(st100, objFont, new SolidBrush(Color.FromArgb(0, 126, 58)), 0, 90);
        objGraphics.DrawString("some site branding", brandFont, new SolidBrush(Color.FromArgb(0, 126, 58)), 100, 120);

        objGraphics.Flush();


        //create the path for the imge to be saved
        string imagePath = System.Web.HttpContext.Current.Server.MapPath("~/MyServerPath/" + sURL + ".png");
        objBmpImage.Save(imagePath);

        //return the imagePath
        return imagePath;
有谁能建议一种方法来创建png的高度和宽度更动态,以便适合字符串长度,并优雅地包装(在分词时)


我已经进行了多次搜索,但找不到任何与我所要搜索的内容完全相符的内容。

您应该研究此方法,它允许您以像素为单位测量给定字体中特定字符串的宽度和高度

您应该研究此方法,它允许您以像素为单位测量给定字体中特定字符串的宽度和高度

谢谢@Eden-我已经测量了4个字符串中的每一个,然后将png宽度设置为最长长度。这是一个合理的临时解决方案,但让我担心的是,如果字符串很长(或很短),那么将其任意切割为4可能不合适,而且我不会考虑换行时的分词。我的直觉是,应该有一个更优雅、更合适的解决方案?只有当文本超过一定的大小时,你才能打断它。你不必打破每一根绳子。您还可以使用DrawText将字符串强制放入特定的矩形中(并进行自动扭曲)。请参阅本文Thank@Eden-我已经测量了4个字符串中的每一个,然后将png宽度设置为最长长度。这是一个合理的临时解决方案,但让我担心的是,如果字符串很长(或很短),那么将其任意切割为4可能不合适,而且我不会考虑换行时的分词。我的直觉是,应该有一个更优雅、更合适的解决方案?只有当文本超过一定的大小时,你才能打断它。你不必打破每一根绳子。您还可以使用DrawText将字符串强制放入特定的矩形中(并具有自动扭曲功能),请参阅本文