C# 如何在c的控制台应用程序中将位图保存为png#
我正在用c#做一个控制台应用程序。在这个应用程序中,我必须创建一个png类型的位图,并且它必须存储在某个定义的路径中(比如C:或D:驱动器) 在windows应用程序中,我使用以下代码创建位图,它将显示在图片框中C# 如何在c的控制台应用程序中将位图保存为png#,c#,bitmap,png,console-application,C#,Bitmap,Png,Console Application,我正在用c#做一个控制台应用程序。在这个应用程序中,我必须创建一个png类型的位图,并且它必须存储在某个定义的路径中(比如C:或D:驱动器) 在windows应用程序中,我使用以下代码创建位图,它将显示在图片框中 void CreateBitmap() { System.Drawing.Bitmap flag = new System.Drawing.Bitmap(10, 10); for( int x = 0; x < flag.Height; ++x ) f
void CreateBitmap()
{
System.Drawing.Bitmap flag = new System.Drawing.Bitmap(10, 10);
for( int x = 0; x < flag.Height; ++x )
for( int y = 0; y < flag.Width; ++y )
flag.SetPixel(x, y, Color.White);
for( int x = 0; x < flag.Height; ++x )
flag.SetPixel(x, x, Color.Red);
pictureBox1.Image = flag;
}
void CreateBitmap()
{
System.Drawing.Bitmap标志=新的System.Drawing.Bitmap(10,10);
对于(int x=0;x
如何使用控制台应用程序在指定的路径中创建和存储此文件
我已按以下方式更改代码,但仍然存在错误:
static void CreatePng(string[] binvalues)
{
String aName = System.Reflection.Assembly.GetExecutingAssembly().Location;
String aPath = System.IO.Path.GetDirectoryName(aName);
string[] ExecDirectories = System.IO.Directory.GetDirectories(aPath);
System.Drawing.Bitmap flag = new System.Drawing.Bitmap(10, 10);
for (int x = 0; x < flag.Height; ++x)
for (int y = 0; y < flag.Width; ++y)
flag.SetPixel(x, y, Color.White);
for (int x = 0; x < flag.Height; ++x)
flag.SetPixel(x, x, Color.Red);
flag.Save(aPath, System.Drawing.Imaging.ImageFormat.Png);
}
static void CreatePng(字符串[]binvalues)
{
字符串aName=System.Reflection.Assembly.GetExecutionGassembly().Location;
字符串aPath=System.IO.Path.GetDirectoryName(aName);
字符串[]ExecDirectories=System.IO.Directory.GetDirectories(aPath);
System.Drawing.Bitmap标志=新的System.Drawing.Bitmap(10,10);
对于(int x=0;x
它在最后一行显示运行时错误,其中flag.save似乎有问题?使用相同的代码,而不是分配给
PictureBox
,调用位图上的save()
方法:
flag.Save("yourpath", System.Drawing.Imaging.ImageFormat.Png);
请注意,您可能必须在控制台应用程序中添加对
System.Drawing
的引用,因为默认情况下它不在那里。现在,我自己从未使用C做过这件事,因此可能有更倾向于使用C的人可以提供帮助,但我能找到以下内容
1) 您需要将图像数据存储在位图中。从您使用flag
2) 您需要调用位图上的save()
函数:
flag.Save(filename, ImageFormat.Png);
3) filename
将是您定义的字符串。这相当容易,因为您只需让应用程序提示用户输入路径并保存即可
有问题吗
免责声明:我从收到我的信息。关于保存可以从中挖掘的png的“正确”方法有很多讨论。使用以下方法:
flag.Save(filename, System.Drawing.Imaging.ImageFormat.Png);
您不需要将图像添加到图片盒中。您有一个控制台应用程序,PictureBox
是一个可视控件
对于将来的问题,如果你花最少的时间先搜索SO或其他来源以获得答案,那就太好了
问题没有正确标记。如果要保存System.Drawing.Bitmap
,则无法对其进行标记BitmapImage
,因为这仅适用于WPF。我改变了这个
您可以尝试以下代码-:
public bool ResizeImage(string OriginalFilepath, string NewFilepath)
{
Bitmap original = (Bitmap)Image.FromFile(OriginalFilepath);
Bitmap resized = new Bitmap(original, new Size(Width,Height));
resized.Save(NewFilepath.png);
}
@Marteen:你至少可以努力把这个链接打包成一个好的评论…虽然我同意这个链接和它的目的,但是你的评论很糟糕。你能帮我们发布运行时错误吗?应该有一个堆栈跟踪,详细说明问题所在。。。。可能缺少目录、格式错误或找不到文件?其他问题?引发的错误是:“System.Drawing.dll中发生了类型为'System.Runtime.Interopservices.ExternalException'的未处理异常”附加信息:GDI+中发生了一般错误您是将其保存到新文件还是覆盖旧文件?如果你试图保存一个正在使用的文件,就会出现这样的异常。编辑:您保存到的路径也可能不存在。您是否正在检查路径是否有效?文件名在运行时未被使用。它是一个新文件,但donno.,仍然会引发错误再次查看代码时,您正在保存到aPath
但没有文件名?尝试flag.Save(aPath+“image.png”,ImageFormat.png)
并查看这是否有帮助。