ConstrainedCopy работает не только с типами-примитивамиНо у нее есть один минус - она работает только с типами-примитивами
ANDLL писал(а):у тебя просто кривые руки
На:ANDLL писал(а):Покажи код
Private _ImgArr() As Image
...
Private Sub ...
...
Array.ConstrainedCopy(_ImgArr, OldIndex + 1, _ImgArrL, OldIndex, _ImgArr.Length - OldIndex - 1)
ReDim Preserve _ImgArr(_ImgArr.Length - 2)
...
End sub
Array.ConstrainedCopy will only work on array types that are provably compatible, without any form of boxing, unboxing, widening, or casting of each array element. Change the array types (i.e., copy a Derived[] to a Base[]), or use a mitigation strategy in the CER for Array.Copy's less powerful reliability contract, such as cloning the array or throwing away the potentially corrupt destination array.
Там где нельзя Image, можно выполнить несложную технику кодерского карате Image > Stream > ToArray
Imports System.IO
Public Class Form1
Private Const url As String = "http://bbs.vbstreets.ru/download/file.php?avatar=11295_1230764984.png"
Private img As Image
Private WithEvents a As New Async
Private IsPicDownloaded As Boolean
'вот как бы сюда нам и надо попасть после изменения img`а. Нужен не факт того, что AsyncWorker скачал, а то что кто-то изменил img
Private Sub ImgChanged()
IsPicDownloaded = True
Me.Invalidate()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
a.DownLoadPic(url, img)
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
If IsPicDownloaded Then
If Not img Is Nothing Then e.Graphics.DrawImageUnscaled(img, 10, 10)
End If
End Sub
End Class
Public Class Async
Public Class AsyncWorker
Implements IDisposable
Private _myId As Integer, dl As String
'TODO: А как объявить переменную, что бы переданный Image в Sub New можно было юзать и изменять в других функциях?
Dim _img As Image
Dim WithEvents wc As New Net.WebClient
Dim ms As MemoryStream
Dim mw As BinaryWriter
Public Event Complete(ByVal myId As Integer)
Public Event PicDownloaded(ByVal Img As Byte(), ByVal id As Integer)
Public Sub New(ByVal myId As Integer, ByVal img As Image, ByVal Url As Uri)
Randomize()
_myId = myId
_img = img
dl = Url.AbsoluteUri
wc.DownloadDataAsync(Url)
End Sub
Public ReadOnly Property URL() As String
Get
Return dl
End Get
End Property
#Region " IDisposable Support "
Private disposedValue As Boolean = False ' To detect redundant calls
' IDisposable
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
' TODO: free other state (managed objects).
' t.Dispose()
wc.Dispose()
mw = Nothing
ms.Dispose()
End If
' TODO: free your own state (unmanaged objects).
' TODO: set large fields to null.
End If
Me.disposedValue = True
End Sub
' This code added by Visual Basic to correctly implement the disposable pattern.
Public Sub Dispose() Implements IDisposable.Dispose
' Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above.
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
#End Region
Private Sub wc_DownloadDataCompleted(ByVal sender As Object, ByVal e As System.Net.DownloadDataCompletedEventArgs) Handles wc.DownloadDataCompleted
If Not e.Cancelled Then
ms = New MemoryStream : mw = New BinaryWriter(ms)
Try
mw.Write(e.Result)
ms.Position = 0
_img = Image.FromStream(ms)
RaiseEvent PicDownloaded(e.Result, _myId)
Catch ex As Exception
End Try
End If
RaiseEvent Complete(_myId)
End Sub
End Class
Private aw() As AsyncWorker
Private _url As String
Public Event PicDownloaded(ByVal Pic As Byte(), ByVal name As String)
Public Sub DownLoadPic(ByVal URL As String, ByVal img As Image)
Dim added As Boolean = False
Dim i As Integer
_url = URL
Try
For i = 0 To aw.Length - 1
If IsNothing(aw(i)) Then
aw(i) = New AsyncWorker(i, img, New Uri(_url))
AddHandler aw(i).Complete, AddressOf DisposeWorker
AddHandler aw(i).PicDownloaded, AddressOf Pic1
added = True
Exit For
End If
Next
If Not added Then
ReDim Preserve aw(aw.Length)
aw(aw.Length - 1) = New AsyncWorker(i, img, New Uri(_url))
AddHandler aw(i).Complete, AddressOf DisposeWorker
AddHandler aw(i).PicDownloaded, AddressOf Pic1
End If
Catch ex As UriFormatException
MsgBox(ex.Message, MsgBoxStyle.Exclamation)
End Try
End Sub
Private Sub DisposeWorker(ByVal Id As Integer)
RemoveHandler aw(Id).Complete, AddressOf DisposeWorker
RemoveHandler aw(Id).PicDownloaded, AddressOf Pic1
aw(Id).Dispose()
aw(Id) = Nothing
End Sub
Private Sub Pic1(ByVal arr As Byte(), ByVal Id As Integer)
RaiseEvent PicDownloaded(arr, IO.Path.GetFileName(aw(Id).URL))
End Sub
Public Sub New()
ReDim aw(-1)
End Sub
End Class
Сейчас этот форум просматривают: нет зарегистрированных пользователей и гости: 27