笔记10: 变换图像

前端之家收集整理的这篇文章主要介绍了笔记10: 变换图像前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


一、显示图形图像
对图像文件显示过程:
(1)创建Image类(如Bitmap)、Graphics烊的对象
(2)用Graphics.DrawImage绘制

图形文件格式:
Bmp 与存储设置和应用程序无关的图像。每位像素位数(1、4、8、15、24、32、64)在
文件头中指定。24位Bmp是通用的。
GIF Web通用格式。适用线、纯色、边界清晰图片。是无损压缩。可指定一色为透明。
JPEG 24位,有损压缩(肉眼难辨),不支持透明。
EXIF 数码相机照片格式,据JPEG压缩,还包括日期、曝光时间、相机等信息。
PNG 无损压缩(同GIF),以8、24、48位存储颜色,并以1、2、4、8、16位存储灰度
为每像素存储Alpha值。还可含Gamma及颜色校正,可精确显示图像。


TIFF 各平台及程序均支持,以每像素任意位存储图像,压缩算法多样。灵活性好


'01.DrawImage绘制图片文件
'   共有30个重载。从原始图上的一部分或全部   画到  目标上的某位置或区域
Imports System.Drawing
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click
        Dim gr As Graphics = Me.CreateGraphics
        Dim bm As New Bitmap("D:\1.jpg")

        '指定位置画原始图的全部
        gr.DrawImage(bm,New Point(10,5))

        '指定位置区域画原始图(有缩放功能)
        gr.TranslateTransform(200,5)
        gr.DrawImage(bm,New Rectangle(0,80,80))

        '取原始图部分进行画(无缩放)
        gr.TranslateTransform(100,0)
        gr.DrawImage(bm,New RectangleF(0,30,60,60),GraphicsUnit.Pixel)

        '取原始图部分在目标上某区域绘制(缩放)
        gr.TranslateTransform(-280,150)
        gr.DrawImage(bm,100,110),GraphicsUnit.Pixel)
    End Sub
End Class







'02.Clone 克隆
'克隆受参数像素格式影响,成图不同
Imports System.Drawing
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click
        Dim gr As Graphics = Me.CreateGraphics
        Dim bm As New Bitmap("D:\1.bmp")

        '原图
        gr.DrawImage(bm,5))

        Dim bm1 As Bitmap = bm.Clone(New Rectangle(0,40,50),Imaging.PixelFormat.Format24bppRgb)
        gr.TranslateTransform(180,5)
        gr.DrawImage(bm1,0) '为了看清clone不再缩放

        Dim bm2 As Bitmap = bm.Clone(New Rectangle(40,Imaging.PixelFormat.Format24bppRgb)
        gr.TranslateTransform(80,0)
        gr.DrawImage(bm2,0)

        '下面是像素不同时的展现
        Dim bm3 As Bitmap = bm.Clone(New Rectangle(40,Imaging.PixelFormat.Format16bppArgb1555)
        gr.TranslateTransform(-250,150)
        gr.DrawImage(bm3,0)

        Dim bm4 As Bitmap = bm.Clone(New Rectangle(40,Imaging.PixelFormat.Format4bppIndexed)
        gr.TranslateTransform(110,0)
        gr.DrawImage(bm4,0)

        Dim bm5 As Bitmap = bm.Clone(New Rectangle(40,Imaging.PixelFormat.Format1bppIndexed)
        gr.TranslateTransform(110,0)
        gr.DrawImage(bm5,0)

        Dim bm6 As Bitmap = bm.Clone(New Rectangle(40,Imaging.PixelFormat.Format8bppIndexed)
        gr.TranslateTransform(110,0)
        gr.DrawImage(bm6,0)
    End Sub
End Class







'03 MakeTransparent 使某色透明
'   先取得某颜色GetPixel,然后用maketransparent进行透明

Imports System.Drawing.Drawing2D
Public Class Form1
    '为了便于查看,窗体背景设置为黄色
    Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click
        Dim gr As Graphics = Me.CreateGraphics
        '取色
        Dim bm As New Bitmap("D:\2.bmp")
        Dim col As Color = bm.GetPixel(1,1)

        '画原始图
        gr.DrawImage(bm,10,10)

        '透明
        bm.MakeTransparent(col)
        gr.DrawImage(bm,280,10)
        gr.DrawRectangle(Pens.Red,New Rectangle(280,bm.Width,bm.Height)) '原来的外框
    End Sub
End Class








'04 用ico图像绘制图形
'  先用ico构造TextureBrush,然后再构造Pen,最后画图形
Imports System.Drawing
Imports System.Drawing.Drawing2D

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click
        Dim gr As Graphics = Me.CreateGraphics
        Dim bm As New Bitmap("d:\3.ico")

        Dim tb As New Drawing.TextureBrush(bm)
        Dim myPen As New Pen(tb,20)

        gr.DrawImage(bm,10)
        gr.DrawEllipse(myPen,New Rectangle(40,200,100))
    End Sub
End Class








二、图像变换

1、Image对象的RotateFlip方法可以旋转和翻转。
Rotate旋转, Flip翻转
须先用Image.FromFile(Str)等方法加载图像文件


'05.图像的旋转与翻转:Image.RotateFlip
'旋转都是顺时针


Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click
        Dim gr As Graphics = Me.CreateGraphics
        Dim img As Image = Image.FromFile("D:\5.jpg")

        gr.DrawImage(img,New Point(0,0))

        img.RotateFlip(RotateFlipType.Rotate90FlipNone) '2.顺时针转90
        gr.DrawImage(img,New Point(130,0))

        '每次变换都是从上一次变换
        img.RotateFlip(RotateFlipType.Rotate90FlipX) '3.先转90再X翻转
        gr.DrawImage(img,New Point(260,0))

        img.RotateFlip(RotateFlipType.Rotate90FlipY) ' 4.先转90再Y翻转
        gr.DrawImage(img,New Point(390,0))

        img.RotateFlip(RotateFlipType.RotateNoneFlipX) ' 5.X翻转
        gr.DrawImage(img,130))

        img.RotateFlip(RotateFlipType.RotateNoneFlipY) '6.Y翻转
        gr.DrawImage(img,130))

        img.Save("D:\11.bmp") '最后形变
    End Sub
End Class








'06.对图形的反射、扭曲
'关键在定义目标区域,目标区域由平行四边形定义(取三个点,第四点自动计算)
'三点:左上角、左下角,右上角。
Imports System.Drawing
Imports System.Drawing.Drawing2D
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click
        Dim bm As New Bitmap("D:\1.jpg")
        Dim gr As Graphics = Me.CreateGraphics

        Dim bm1 As New Bitmap(bm.Width,bm.Height)
        Dim gr1 As Graphics = Graphics.FromImage(bm1)
        
        Dim p As Point() = {New Point(100,0),New Point(200,50)}
        
        gr.DrawImage(bm,0)) '原图在窗体上

        '示意图:bm上有图,通过绘制(变形)到bm1(原先有空白,现在有图),最后显示在窗体上
        gr1.DrawImage(bm,p)  '先把bm变形画到bm1上(要注意bm1能容纳bm变形后的图形,例中专门不能容纳全部变形后图形)
        gr.DrawImage(bm1,150)) '再把bm1画上窗体上

        bm1.Save("D:\11.jpg") '变形图形存盘
    End Sub
End Class








'07.插值模式缩放 Graphics.InterPolationMode

Imports System.Drawing
Imports System.Drawing.Drawing2D
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click
        Dim gr As Graphics = Me.CreateGraphics
        Dim bm As New Bitmap("d:\1.jpg")
        Dim src As New Rectangle(0,bm.Height)
        Dim dec As New Rectangle(0,0.5 * bm.Width,0.5 * bm.Height)

        gr.DrawImage(bm,src,GraphicsUnit.Pixel) '原图

        gr.TranslateTransform(160,dec,GraphicsUnit.Pixel) '缺省

        gr.InterpolationMode = Drawing2D.InterpolationMode.Bicubic
        gr.TranslateTransform(80,GraphicsUnit.Pixel)

        gr.InterpolationMode = Drawing2D.InterpolationMode.High
        gr.TranslateTransform(80,GraphicsUnit.Pixel)

        gr.InterpolationMode = Drawing2D.InterpolationMode.Low
        gr.TranslateTransform(80,GraphicsUnit.Pixel)
    End Sub
End Class









'08.纹理填充图形(如椭圆)
'主要用TextureBrush,并可对纹理缩放
Imports System.Drawing
Imports System.Drawing.Drawing2D
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click
        Dim gr As Graphics = Me.CreateGraphics
        Dim bm As New Bitmap("d:\1.jpg")
        Dim tBrush As New TextureBrush(bm)

        gr.FillEllipse(tBrush,300,150))

        tBrush.ScaleTransform(0.5F,0.5F) '可对纹理进行缩放等坐标变换
        gr.FillEllipse(tBrush,150,150))
    End Sub
End Class









'09.Graphics.BeginContainer 
'   Graphics.EndContainer
'图形容器:“装载”或“容纳”图形数据(包括坐标)的“容器”。比如窗口(0,0)就是默认的容器。
'BeginContainer将保存先前的容器(数据到堆栈上,并设置标识),开始新的一容器。
'EndContainer 结束当前容器,并返回上一个beginContainer保存的数据。
'EndContainer之前可以有多个BeginContainer,但只能返回上一次容器数据(并移除堆栈上数据)
Imports System.Drawing
Imports System.Drawing.Drawing2D
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click
        Dim gr As Graphics = Me.CreateGraphics
        Dim grContainer As GraphicsContainer

        '定位,便于观看后面位置变化
        gr.DrawRectangle(Pens.Black,New Rectangle(50,50,50))

        gr.TranslateTransform(50,50) 'A
        grContainer = gr.BeginContainer '存储以前数据,开始创建容器

        gr.TranslateTransform(0,40)
        gr.FillRectangle(Brushes.Red,20,60))

        gr.EndContainer(grContainer) '恢复存储前容器,所以坐标返回到A处
        gr.FillRectangle(Brushes.Blue,20))
    End Sub
End Class







'10.EndContainer(gc)还原某状态前的容器
'   gr.restore 还原原始容器,删除堆栈上的信息
Imports System.Drawing.Drawing2D
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click
        Dim gr As Graphics = Me.CreateGraphics
        Dim gc1 As GraphicsContainer
        Dim gc2 As GraphicsContainer

        gc1 = gr.BeginContainer
        gr.TranslateTransform(50,50)
        gr.FillRectangle(Brushes.Blue,50))

        gc2 = gr.BeginContainer
        gr.TranslateTransform(50,50)
        gr.FillRectangle(Brushes.Red,60))

        gr.EndContainer(gc2)
        gr.FillRectangle(Brushes.Black,40))

        gr.EndContainer(gc1)
        gr.FillRectangle(Brushes.Red,30))
    End Sub
End Class
原文链接:https://www.f2er.com/vb/258455.html

猜你在找的VB相关文章