知识大全 图片上传的WebForm(自动生成所略图)
Posted 知
篇首语:别裁伪体亲风雅,转益多师是汝师。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 图片上传的WebForm(自动生成所略图)相关的知识,希望对你有一定的参考价值。
图片上传的WebForm(自动生成所略图) 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!
因自己的程序中需对一个窗体区域频繁进行彩色转灰度处理 为此专门写了个函数 处理对象是一块经常变化的动态区域 且是一系列绘图中的一部分 速度要求较高 算法上力求简单 所以采用以下两步方案 基于DDB来写 虽然转入DIB 可不必面对各种色深 会统一算法 但转换过程会让速度上慢很多 再者这只是针对屏幕位图的函数 并无保存需要 考虑实际情况 我只写了 位三种色深下的算法 其实 两种位图是最快的了 不管多大的图只需处理 与 次运算 可是现在哪有人的屏幕 还使用这两种显示模式呢?想想就没这个必要了 相比之下 位时最快 位时最慢 心里有点不满意 但好在速度都不慢 差距也不超过 % 灰度算法本来就不复杂 但我还是做了简化 正常处理时一般需对RGB做加权平均 取个值来统一三基色 但这需涉及浮点运算 速度上不去 效果却不见得有多好 我的方法很简单 就是取三基色之一的值 统一起来 考虑人眼对绿色最敏感 所以算法就成RGB转GGG了 严格的说 这不叫彩转灰 叫绿转灰更合适 RGB的排列G是在中间的 想利用高速Long运算 用B值最快的 但已经够简化了 再简下去 自己都过意不去 (用B值时 位下 速度还可快 / ) 这种算法当然有缺陷 主要是对一些偏色图效果不好 但好在这种情况在色彩丰富的界面中不存在 C G M WinXP SP 下的测试情况 IDE环境下 X 的位图 位屏幕 毫秒 位屏幕 毫秒 N代码编译 全部优化打开 X 的位图 位屏幕 毫秒 位屏幕 毫秒 注 没有 位环境 所以也就没测了 Option Explicit Private Type BITMAP bmType As Long bmWidth As Long bmHeight As Long bmWidthBytes As Long bmPlanes As Integer bmBitsPixel As Integer bmBits As Long End Type Private Type MemHdc hdc As Long Bmp As Long obm As Long End Type Private Declare Function GetObj Lib gdi Alias GetObjectA (ByVal hObject As Long ByVal nCount As Long lpObject As Any) As Long Private Declare Function SelectObject Lib gdi (ByVal hdc As Long ByVal hObject As Long) As Long Private Declare Function DeleteObject Lib gdi (ByVal hObject As Long) As Long Private Declare Function DeleteDC Lib gdi (ByVal hdc As Long) As Long Private Declare Function BitBlt Lib gdi (ByVal hDestDC As Long ByVal x As Long ByVal y As Long ByVal nWidth As Long ByVal nHeight As Long ByVal hSrcDC As Long ByVal xSrc As Long ByVal ySrc As Long ByVal dwRop As Long) As Long Private Declare Function CreateCompatibleDC Lib gdi (ByVal hdc As Long) As Long Private Declare Function CreateCompatibleBitmap Lib gdi (ByVal hdc As Long ByVal nWidth As Long ByVal nHeight As Long) As Long Private Declare Function GetBitmapBits Lib gdi (ByVal hBitmap As Long ByVal dwCount As Long lpBits As Any) As Long Private Declare Function SetBitmapBits Lib gdi (ByVal hBitmap As Long ByVal dwCount As Long lpBits As Any) As Long Private Declare Function GetTickCount Lib kernel () As Long Private Declare Sub CopyMemory Lib kernel Alias RtlMoveMemory (pDest As Any pSource As Any ByVal dwLength As Long) 平时常做图形处理 自己的两个公用函数也就用上了 Private Function NewMyHdc(dHdc As Long w As Long h As Long Optional Bm As Long) As MemHdc With NewMyHdc hdc = CreateCompatibleDC(dHdc) If Bm = Then Bmp = CreateCompatibleBitmap(dHdc w h) Else Bmp = Bm End If obm = SelectObject( hdc Bmp) End With End Function Private Function DelMyHdc(MyHdc As MemHdc Optional nobmp As Boolean) As MemHdc With MyHdc If hdc <> And obm <> Then SelectObject hdc obm If nobmp = False And Bmp <> Then DeleteObject Bmp If hdc <> Then DeleteDC hdc End With End Function 灰度处理主函数 Private Function GrayBmp(dHdc As Long x As Long y As Long w As Long h As Long) As Long Dim tmpdc As MemHdc Dim i As Long j As Long m As Long k As Byte l As Long Dim Bm As BITMAP AllBytes As Long LineBytes As Long Dim dBits() As Byte Dim dBits () As Integer Dim dBits () As Long On Error GoTo last With tmpdc tmpdc = NewMyHdc(dHdc w h) GetObj Bmp Len(Bm) Bm If Bm bmBitsPixel < Then GoTo last BitBlt hdc w h dHdc x y vbSrcCopy LineBytes = Bm bmWidthBytes AllBytes = LineBytes * h Select Case Bm bmBitsPixel Case ReDim dBits (AllBytes \\ ) GetBitmapBits Bmp AllBytes dBits ( ) For i = To AllBytes \\ dBits (i) = ((dBits (i) And &HFF &) \\ &H ) * &H dBits (i) = (dBits (i) And &HFF) * &H 用B值运算 Next SetBitmapBits Bmp AllBytes dBits ( ) GrayBmp = Case ReDim dBits(AllBytes ) GetBitmapBits Bmp AllBytes dBits( ) For j = To h m = j * LineBytes For i = m To m + w * Step dBits(i) = dBits(i + ) dBits(i + ) = dBits(i) Next Next SetBitmapBits Bmp AllBytes dBits( ) GrayBmp = Case 按 格式运算 ReDim dBits (AllBytes \\ ) GetBitmapBits Bmp AllBytes dBits ( ) For j = To h m = j * LineBytes \\ For i = m To m + w l = dBits (i) And &H C & l = l * + l + l \\ CopyMemory dBits (i) l 这句没办法 不用CopyMemory 会溢出 低效源于此 Next Next SetBitmapBits Bmp AllBytes dBits ( ) GrayBmp = End Select BitBlt dHdc x y w h hdc vbSrcCopy End With last: DelMyHdc tmpdc End Function Private Sub Form_Load() ScaleMode = AutoRedraw = True Picture = LoadPicture( f:\\ jpg ) Command Caption = 测试 End Sub 测试用代码 Private Sub Form_Resize() PaintPicture Picture ScaleWidth ScaleHeight End Sub Private Sub Command _Click() Dim t As Long s As String s As String i As Long t = GetTickCount GrayBmp hdc ScaleWidth ScaleHeight Refresh MsgBox GetTickCount t & s End Sub cha138/Article/program/net/201311/13229相关参考