【.Net 6.0--通用帮助类--ConvertHelper】

返回首页

前言

类型转换帮助类,包含下表中的方法:

方法名方法解释
ObjToIntobject转int
ObjToMoneyobject转double
ObjToStringobject转string
ObjToDecimalobject转decimal
ObjToDateobject转datetime
ObjToDateSplitYMDobject转datetime(yyyy-MM-dd)
ObjToDateSplitYMDHMSobject转datetime(yyyy-MM-dd HH:mm:ss)
ObjToDateYobject转datetime(yyyy)
ObjToDateYMDobject转datetime(yyyyMMdd)
ObjToDateYMDHobject转datetime(yyyyMMddHH)
ObjToDateYMDHMSobject转datetime(yyyyMMddHHmmss)
ObjToBoolobject转bool
ObjToCharobject转char
ObjToStringBySplitobject转list
ObjToBase64Stringobject转base64 string
ObjToStringFromBase64base64 string转object

代码示例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VW.API.Common.Utils
{
    /// <summary>
    /// ConvertHelper 的摘要说明:格式转换帮助类
    /// </summary>
    public static class ConvertHelper
    {
        /// <summary>
        /// obj->int
        /// </summary>
        /// <param name="thisValue"></param>
        /// <returns>int</returns>
        public static int ObjToInt(this object thisValue)
        {
            try
            {
                if (thisValue == null)
                {
                    return 0;
                }
                if (thisValue != null && thisValue != DBNull.Value && int.TryParse(thisValue.ToString(), out int reval))
                {
                    return reval;
                }

                return 0;
            }
            catch (Exception) { throw; }
        }

        /// <summary>
        /// obj->double
        /// </summary>
        /// <param name="thisValue"></param>
        /// <returns>double</returns>
        public static double ObjToMoney(this object thisValue)
        {
            try
            {
                if (thisValue == null)
                {
                    return 0;
                }
                if (thisValue != null && thisValue != DBNull.Value && double.TryParse(thisValue.ToString(), out double reval))
                {
                    return reval;
                }

                return 0;
            }
            catch (Exception) { throw; }
        }

        /// <summary>
        /// obj->string
        /// </summary>
        /// <param name="thisValue"></param>
        /// <returns>string</returns>
        public static string ObjToString(this object thisValue)
        {
            try
            {
                if (thisValue == null)
                {
                    return "";
                }
                if (thisValue != null)
                    return thisValue.ToString();

                return "";
            }
            catch (Exception) { throw; }
        }

        /// <summary>
        /// obj->decimal
        /// </summary>
        /// <param name="thisValue"></param>
        /// <returns>decimal</returns>
        public static decimal ObjToDecimal(this object thisValue)
        {
            try
            {
                if (thisValue == null)
                {
                    return 0;
                }
                if (thisValue != null && thisValue != DBNull.Value && decimal.TryParse(thisValue.ToString(), out decimal reval))
                {
                    return reval;
                }

                return 0;
            }
            catch (Exception) { throw; }
        }

        /// <summary>
        /// obj->datetime2
        /// </summary>
        /// <param name="thisValue"></param>
        /// <returns>DateTime</returns>
        public static DateTime ObjToDate2(this object thisValue)
        {
            try
            {
                return thisValue.ToString().Length switch
                {
                    //年
                    4 => new DateTime(thisValue.ObjToInt(), 1, 1, 0, 0, 0),
                    //月
                    6 => new DateTime(thisValue.ToString().Substring(0, 4).ObjToInt(), thisValue.ToString().Substring(4, 2).ObjToInt(), 1, 0, 0, 0),
                    //日
                    8 => new DateTime(thisValue.ToString().Substring(0, 4).ObjToInt(), thisValue.ToString().Substring(4, 2).ObjToInt(), thisValue.ToString().Substring(6, 2).ObjToInt(), 0, 0, 0),
                    //时
                    10 => new DateTime(thisValue.ToString().Substring(0, 4).ObjToInt(), thisValue.ToString().Substring(4, 2).ObjToInt(), thisValue.ToString().Substring(6, 2).ObjToInt(), thisValue.ToString().Substring(8, 2).ObjToInt(), 0, 0),
                    //分钟
                    12 => new DateTime(thisValue.ToString().Substring(0, 4).ObjToInt(), thisValue.ToString().Substring(4, 2).ObjToInt(), thisValue.ToString().Substring(6, 2).ObjToInt(), thisValue.ToString().Substring(8, 2).ObjToInt(), thisValue.ToString().Substring(10, 2).ObjToInt(), 0),
                    _ => DateTime.MinValue,
                };
            }
            catch (Exception) { throw; }
        }

        /// <summary>
        /// obj->datetime
        /// </summary>
        /// <param name="thisValue"></param>
        /// <returns>DateTime</returns>
        public static DateTime ObjToDate(this object thisValue)
        {
            try
            {
                DateTime reval = DateTime.MinValue;
                if (thisValue == null)
                {
                    return reval;
                }
                if (thisValue != null && thisValue != DBNull.Value && DateTime.TryParse(thisValue.ToString(), out reval))
                {
                    reval = Convert.ToDateTime(thisValue);
                }

                return reval;
            }
            catch (Exception) { throw; }
        }

        /// <summary>
        /// obj->datetime(SplitYMD)
        /// </summary>
        /// <param name="thisValue"></param>
        /// <returns>DateTime</returns>
        public static string ObjToDateSplitYMD(this object thisValue)
        {
            try
            {
                if (thisValue == null)
                {
                    return "";
                }

                return ObjToDate(thisValue).ToString("yyyy-MM-dd");
            }
            catch (Exception) { throw; }
        }

        /// <summary>
        /// obj->datetime(SplitYMDHMS)
        /// </summary>
        /// <param name="thisValue"></param>
        /// <returns>DateTime</returns>
        public static string ObjToDateSplitYMDHMS(this object thisValue)
        {
            try
            {
                if (thisValue == null)
                {
                    return "";
                }

                return ObjToDate(thisValue).ToString("yyyy-MM-dd HH:mm:ss");
            }
            catch (Exception) { throw; }
        }

        /// <summary>
        /// obj->datetime(Y)
        /// </summary>
        /// <param name="thisValue"></param>
        /// <returns>DateTime</returns>
        public static string ObjToDateY(this object thisValue)
        {
            try
            {
                if (thisValue == null)
                {
                    return "";
                }

                return ObjToDate(thisValue).ToString("yyyy");
            }
            catch (Exception) { throw; }
        }

        /// <summary>
        /// obj->datetime(YMD)
        /// </summary>
        /// <param name="thisValue"></param>
        /// <returns>DateTime</returns>
        public static string ObjToDateYMD(this object thisValue)
        {
            try
            {
                if (thisValue == null)
                {
                    return "";
                }

                return ObjToDate(thisValue).ToString("yyyyMMdd");
            }
            catch (Exception) { throw; }
        }

        /// <summary>
        /// obj->datetime(YMDH)
        /// </summary>
        /// <param name="thisValue"></param>
        /// <returns>DateTime</returns>
        public static string ObjToDateYMDH(this object thisValue)
        {
            try
            {
                if (thisValue == null)
                {
                    return "";
                }

                return ObjToDate(thisValue).ToString("yyyyMMddHH");
            }
            catch (Exception) { throw; }
        }

        /// <summary>
        /// obj->datetime(YMDHMS)
        /// </summary>
        /// <param name="thisValue"></param>
        /// <returns>DateTime</returns>
        public static string ObjToDateYMDHMS(this object thisValue)
        {
            try
            {
                if (thisValue == null)
                {
                    return "";
                }

                return ObjToDate(thisValue).ToString("yyyyMMddHHmmss");
            }
            catch (Exception) { throw; }
        }

        /// <summary>
        /// obj->bool
        /// </summary>
        /// <param name="thisValue"></param>
        /// <returns>bool</returns>
        public static bool ObjToBool(this object thisValue)
        {
            try
            {
                if (thisValue == null)
                {
                    return false;
                }
                if (thisValue != null && thisValue != DBNull.Value && bool.TryParse(thisValue.ToString(), out bool reval))
                {
                    return reval;
                }

                return false;
            }
            catch (Exception) { throw; }
        }

        /// <summary>
        /// obj->char
        /// </summary>
        /// <param name="thisValue"></param>
        /// <returns>char</returns>
        public static char ObjToChar(this object thisValue)
        {
            try
            {
                if (thisValue == null)
                {
                    return ' ';
                }
                if (thisValue != null && thisValue != DBNull.Value && char.TryParse(thisValue.ToString(), out char reval))
                {
                    return reval;
                }

                return ' ';
            }
            catch (Exception) { throw; }
        }

        /// <summary>
        /// obj->List<string>
        /// </summary>
        /// <param name="thisValue"></param>
        /// <param name="split"></param>
        /// <returns>List<string></returns>
        public static List<string> ObjToStringBySplit(this object thisValue, char[] split = null)
        {
            try
            {
                if (thisValue == null)
                {
                    return new List<string>();
                }

                if (split == null || split.Length == 0)
                {
                    split = new char[] { ';', ';', ',', ',', ' ' };
                }

                return thisValue.ToString().Split(split).ToList();
            }
            catch (Exception) { throw; }
        }

        /// <summary>
        /// obj->string<string>
        /// </summary>
        /// <param name="thisValue"></param>
        /// <param name="split"></param>
        /// <returns>List<string></returns>
        public static string ObjToStringByReplace(this object thisValue, string[] replace = null)
        {
            try
            {
                if (thisValue == null)
                {
                    return "";
                }

                //-()&"*%()
                if (replace == null || replace.Length == 0)
                {
                    replace = new string[] { "-", "(", ")", "&", "\"", "*", "%", "(", ")", "_" };
                }

                string returnValue = thisValue.ToString();
                foreach (string r in replace)
                {
                    returnValue = returnValue.Replace(r, $"\\{r}");
                }

                return returnValue;
            }
            catch (Exception) { throw; }
        }

        /// <summary>
        /// obj->base64string
        /// </summary>
        /// <param name="thisValue"></param>
        /// <returns>string</returns>
        public static string ObjToBase64String(this object thisValue)
        {
            try
            {
                if (thisValue == null)
                {
                    return "";
                }
                if (thisValue != null)
                    return Convert.ToBase64String(Encoding.UTF8.GetBytes($"{thisValue}"));

                return "";
            }
            catch (Exception) { throw; }
        }

        /// <summary>
        /// obj->string
        /// </summary>
        /// <param name="thisValue"></param>
        /// <returns>string</returns>
        public static string ObjToStringFromBase64(this object thisValue)
        {
            try
            {
                if (thisValue == null)
                {
                    return "";
                }
                if (thisValue != null)
                    return System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(thisValue.ToString()));

                return "";
            }
            catch (Exception) { throw; }
        }
    }
}
  • 17
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
.NET 6.0中,可以使用Windows API来调用摄像头进行拍照和录制视频。具体步骤如下: 1. 引用相关命名空间:需要使用Windows API进行摄像头的调用,需要在代码文件中添加以下命名空间: ```csharp using System.Runtime.InteropServices; using System.Diagnostics; using System.Windows.Forms; ``` 2. 调用摄像头进行拍照:使用Windows API中的SendMessage方法指定命令来调用摄像头,如下所示: ```csharp [DllImport("user32.dll", EntryPoint = "SendMessage")] public static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam); const uint WM_CAP_START = 0x400; const uint WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10; const uint WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11; const uint WM_CAP_SAVEDIB = WM_CAP_START + 25; const int IDOK = 1; public void TakePhoto() { ProcessStartInfo startInfo = new ProcessStartInfo("C:\\Windows\\System32\\amcap.exe"); // 打开摄像头软件 Process.Start(startInfo); IntPtr hWnd = IntPtr.Zero; while (hWnd.ToInt32() == 0) { hWnd = FindWindow(null, "AMCap"); // 查找摄像头软件的窗口句柄 Thread.Sleep(100); } SendMessage(hWnd.ToInt32(), WM_CAP_DRIVER_CONNECT, 0, 0); // 连接摄像头 SendMessage(hWnd.ToInt32(), WM_CAP_SAVEDIB, 0, 0); // 拍照 SendMessage(hWnd.ToInt32(), WM_CAP_DRIVER_DISCONNECT, 0, 0); // 断开连接摄像头 } ``` 3. 调用摄像头进行录制视频:同样是使用Windows API中的SendMessage方法指定命令来调用摄像头,如下所示: ```csharp [DllImport("user32.dll", EntryPoint = "SendMessage")] public static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam); const uint WM_CAP_START = 0x400; const uint WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10; const uint WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11; const uint WM_CAP_SET_PREVIEW = WM_CAP_START + 50; const uint WM_CAP_SET_PREVIEWRATE = WM_CAP_START + 52; const uint WM_CAP_SAVEDIB = WM_CAP_START + 25; const int IDOK = 1; public void RecordVideo() { ProcessStartInfo startInfo = new ProcessStartInfo("C:\\Windows\\System32\\amcap.exe"); // 打开摄像头软件 Process.Start(startInfo); IntPtr hWnd = IntPtr.Zero; while (hWnd.ToInt32() == 0) { hWnd = FindWindow(null, "AMCap"); // 查找摄像头软件的窗口句柄 Thread.Sleep(100); } SendMessage(hWnd.ToInt32(), WM_CAP_DRIVER_CONNECT, 0, 0); // 连接摄像头 SendMessage(hWnd.ToInt32(), WM_CAP_SET_PREVIEW, 0, 0); // 设置预览 SendMessage(hWnd.ToInt32(), WM_CAP_SET_PREVIEWRATE, 66, 0); // 设置预览帧率 string fileName = "C:\\video.avi"; if (File.Exists(fileName)) File.Delete(fileName); SendMessage(hWnd.ToInt32(), WM_CAP_SAVEDIB, 0, fileName); // 开始录制 MessageBox.Show("按OK停止录制", "提示", MessageBoxButtons.OK); SendMessage(hWnd.ToInt32(), WM_CAP_SAVEDIB, 0, 0); // 停止录制 SendMessage(hWnd.ToInt32(), WM_CAP_DRIVER_DISCONNECT, 0, 0); // 断开连接摄像头 } ``` 需要注意的是,这两个方法都是在Windows环境下调用摄像头软件进行实现的,因此需要保证在调用的计算机上已经安装了能够调用摄像头的摄像头软件。如果想要在跨平台的环境中使用摄像头,可能需要使用其他的第三方库或者API进行实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

丿Nohedid灬山羊

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

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

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

打赏作者

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

抵扣说明:

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

余额充值