| 网站首页 | VB.Net实例分析 | .Net技术文摘 | 下载中心 | VB.Net书籍笔记 | .Net源码 | VBS脚本与组件调用 | Windows2008技术文摘 | 给我们留言 | 
最新公告:

  没有公告

您现在的位置: 乐博网 >> VB.Net实例分析 >> 图形编程 >> 实例分析正文
最新推荐 更多内容
反转图片颜色的实例
反转图片颜色的实例
作者:Larry Nung    来源:Level Up     更新时间:2009-11-14

本文代码实现的功能:如何把彩色图片转为黑白图片。欲把彩色图片转为黑白图片,我们可以把图片上的每个像素都设为灰阶值。灰阶值的取得可套用下面公式。

灰阶值 = (R+G+B)/3 

范例程式

VB.NET

'|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'Author: Larry Nung
'Date: 2009/6/2
'File: 
'Memo: 
'|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
''' <summary>
''' 
''' </summary>
''' <remarks></remarks>
Public Class Form1
 
 
    '***************************************************************************
    'Author: Larry Nung
    'Date: 2009/6/2
    'Purpose: 
    'Memo: 
    '***************************************************************************
    ''' <summary>
    ''' Handles the Click event of the Button1 control.
    ''' </summary>
    ''' <param name="sender">The source of the event.</param>
    ''' <param name="e">The <see cref="System.EventArgs" /> instance containing the event data.</param>
    ''' <remarks></remarks>
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            Me.PictureBox1.Image = GetGrayBitmap(OpenFileDialog1.FileName)
        End If
    End Sub  '更多.net源码和教程,来自[乐博网 www.lob.cn]
 
 
    '***************************************************************************
    'Author: Larry Nung
    'Date: 2009/6/2
    'Purpose: 
    'Memo: 
    '***************************************************************************
    ''' <summary>
    ''' Gets the gray image.
    ''' </summary>
    ''' <param name="file">The file.</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function GetGrayBitmap(ByVal file As String) As Bitmap
        Dim bmp As Bitmap = New Bitmap(file)
        For x As Integer = 0 To bmp.Width - 1
            For y As Integer = 0 To bmp.Height - 1
                Dim color As Color = bmp.GetPixel(x, y)
                Dim gray As Integer = (CInt(color.R) + CInt(color.G) + CInt(color.B)) \ 3
                bmp.SetPixel(x, y, color.FromArgb(gray, gray, gray))
            Next
        Next
        Return bmp
    End Function
 
End Class

 

C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace WindowsFormsApplication1
{
 
 
    //|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
    //Author: Larry Nung
    //Date: 2009/6/2
    //File: 
    //Memo: 
    //|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
    /// <summary>
    /// 
    /// </summary>
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
 
        //***************************************************************************
        //Author: Larry Nung
        //Date: 2009/6/2
        //Purpose: 
        //Memo: 
        //***************************************************************************
        /// <summary>
        /// Handles the Click event of the button1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                pictureBox1.Image = GetGrayBitmap(openFileDialog1.FileName);
            }
        }  '更多.net源码和教程,来自[乐博网 www.lob.cn]
 
 
        //***************************************************************************
        //Author: Larry Nung
        //Date: 2009/6/2
        //Purpose: 
        //Memo: 
        //***************************************************************************
        /// <summary>
        /// Gets the gray bitmap.
        /// </summary>
        /// <param name="file">The file.</param>
        /// <returns></returns>
        private Bitmap GetGrayBitmap(string file)
        {
            Bitmap bmp = new Bitmap(file);
            for (int x=0; x < bmp.Width; x++)
            {
                for (int y=0; y < bmp.Height; y++)
                {
                    Color color = bmp.GetPixel(x, y);
                    int gray = (color.R + color.G + color.B) / 3;
                    bmp.SetPixel(x, y, Color.FromArgb(gray, gray, gray));
                }
            }
            return bmp;
        }
    }
}

  • 上一篇:

  • 下一篇:
  • 【字体: 】【打印此文】【关闭窗口
      相关文章:(只显示最新16条)
    VB.Net跑马灯的实例
    调色盘RGB颜色表的实例
    RGB颜色滚动条变动的实例
    VB.Net画图的实例
    VB.Net颜色表的实例

    | 设为首页 | 加入收藏 | 联系站长 | | 友情链接 | 版权申明 |
    乐博网欢迎各种媒体转载我们的原创作品[转载请注明出处];我们鼓励更多VB.Net开发者一起加入研究与探讨;如发现文章访问错误、内容错误或版权疑问、内容有违相关法律(如涉及政治、色情、反动或散布虚假有害信息)等情况,请及时向我们举报,我们将及时纠正!
    联系邮箱:Shiny#vip.qq.com (#替换为@) QQ交流群: 40797788 [闽ICP备05014267号]