如何获取所有颜色值-.NET颜色集合与ASP.NET示例代码实现

教程大全 2026-03-01 07:42:27 浏览

在 ASP.NET 中获取所有系统预定义的颜色值,可以通过遍历 KnownColor 枚举实现,以下是两种常用方法(Web Forms 和 MVC):

方法 1:ASP.NET Web Forms 示例

// ColorsPage.aspx.csusing System;using System.Drawing;using System.Web.UI.WebControls;public partial class ColorsPage : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){// 获取所有非系统颜色(排除菜单高亮等动态颜色)foreach (KnownColor kc in Enum.GetValues(typeof(KnownColor))){Color color = Color.FromKnownColor(kc);// 跳过系统颜色(如Control, Menu等)if (color.IsSystemColor) continue;// 创建带颜色块的LabelLabel lbl = new Label{Text = $"{color.Name} (#{color.R:X2}{color.G:X2}{color.B:X2})",BackColor = color,ForeColor = Color.Black, // 文字颜色BorderStyle = BorderStyle.Solid,BorderWidth = 1,Width = 200,Height = 30,Style = "margin:5px; padding:5px; display:inline-block;"};// 添加到页面容器container.Controls.Add(lbl);}}}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ColorsPage.aspx.cs" Inherits="YourNamespace.ColorsPage" %>所有系统颜色

方法 2:ASP.NET MVC 示例

// ColorsController.csusing System.Collections.Generic;using System.Drawing;using System.Web.Mvc;public class ColorsController : Controller{public ActionResult Index(){var colors = new List();foreach (KnownColor kc in Enum.GetValues(typeof(KnownColor))){Color color = Color.FromKnownColor(kc);if (color.IsSystemColor) continue;colors.Add(new ColorInfo{Name = color.Name,HexCode = $"#{color.R:X2}{color.G:X2}{color.B:X2}",ColorObject = color});}return View(colors);}}public class ColorInfo{public string Name { get; set; }public string HexCode { get; set; }public Color ColorObject { get; set; }}
@* Views/Colors/Index.cshtml *@@model List

系统预定义颜色

@foreach (var item in Model){
@item.Name (@item.HexCode)
}

关键说明:

输出效果示例:

注意事项:

通过以上方法,您可以在 ASP.NET 应用中完整展示所有系统预定义的颜色及其十六进制值。


用c++编写的读取bmp图中任意一像素点的RGB值的代码

如何获取所有颜色值

控制台 读取BMP图片文件 输出指定点的颜色分量 #include #include #include typedef unsigned long DWORD; typedef int BOOL; typedef unsigned char BYTE; typedef unsigned short WORD; typedef struct tagBITMAPINFOHEADER{DWORD biSize;long biWidth;long biHeight;WORD biPlanes;WORD biBitCount;DWORD biCompression;DWORD biSizeImage;long biXPelsPerMeter;long biYPelsPerMeter;DWORD biClrUsed;DWORD biClrImportant; } BITMAPINFOHEADER; int ReadBmp(const char* szFileName); int GetDIBColor(int X, int Y, BYTE *r, BYTE *g, BYTE *b); BITMAPINFOHEADER bih; BYTE *Buffer = NULL; long LineByteWidth; int main(void) { int x, y; BYTE r, g, b; int n; char szfilename[255] = c:\\; if (ReadBmp(szfilename) == 0) {printf(failure to read file %s, szfilename);return 1; } printf(Width: %ld\n, ); printf(Height: %ld\n, ); printf(BitCount: %d\n\n, (int)); while(1) {printf(input the X:);sCanf(%d, &x);if (x < 0)break;printf(input the Y:);scanf(%d, &y);if (GetDIBColor(x, y, &r, &g, &b) == 1)printf((%d, %d): r:%d, g:%d, b:%d\n, x, y, (int)r, (int)g, (int)b);elseprintf(input error.\n); } free(Buffer); return 0; } int ReadBmp(const char* szFileName) { FILE *file; WORD bfh[7]; long dpixeladd; if (NULL == (file = fopen(szFileName, rb))) {return 0; } printf(%s\n, szFileName); fread(&bfh, sizeof(WORD), 7, file); if (bfh[0] != (WORD)(((WORD)B)|(M<<8))) {fclose(file);return 0; } fread(&bih, sizeof(BITMAPINFOHEADER), 1, file); if ( < 24) {fclose(file);return 0; } dpixeladd = / 8; LineByteWidth = * (dpixeladd); if ((LineByteWidth % 4) != 0) LineByteWidth += 4 - (LineByteWidth % 4); if ((Buffer = (BYTE*)malloc(sizeof(BYTE)* LineByteWidth * )) != NULL) {fread(Buffer, LineByteWidth * , 1, file);fclose(file);return 1; } fclose(file); return 0; } int GetDIBColor(int X, int Y, BYTE *r, BYTE *g, BYTE *b) { int dpixeladd; BYTE *ptr; if (X < 0 || X >= || Y < 0 || Y >= ) {return 0; } dpixeladd = / 8; ptr = Buffer + X * dpixeladd + ( - 1 - Y) * LineByteWidth; *b = *ptr; *g = *(ptr + 1); *r = *(ptr + 2); return 1; }

建立Asp.net程序获取下列的系统环境变量:Remote_addr, local_addr,Request_method

您好,这些变量这样获得:Remote_addr-->[REMOTE_ADDR];local_addr-->[LOCAL_ADDR];Request_method-->[REQUEST_METHOD];完整程序:using System;using ;using ;using ;using ;using ;using ;using ;using ;using ;namespace WebApplication1{ /// /// WebForm1 的摘要说明。 /// public class WebForm1 : {private void Page_Load(object sender, e){ // 在此处放置用户代码以初始化页面 string Remote_addr=[REMOTE_ADDR]; string local_addr=[LOCAL_ADDR]; string Request_method=[REQUEST_METHOD]; (Remote_addr); (); (local_addr); (); (Request_method); ();}#region Web 窗体设计器生成的代码override protected void OnInit(EventArgs e){ // // CODEGEN: 该调用是 Web 窗体设计器所必需的。 // InitializeComponent(); (e);}/// /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 ///private void InitializeComponent(){ += new (_Load);}#endregion }}

asp.net调用js方法的返回值

试试 函数: ((), myscript, ); 变量就用隐藏域并在js里把=_; 1[0](); 后台用request[id]来获取值;

本文版权声明本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,请联系本站客服,一经查实,本站将立刻删除。

发表评论

热门推荐