I have a project which copy varbinary(max) image data from sql server to msaccess ole object and when I am trying to bind that images to image control of VB6 its giving Runtime-error 481: Invalid picture, also I tried saving those msaccess images and they are getting saved fine but when I am trying to load those images in image control they are giving same error, following is my code and attach is my image file
Private Sub Form_Load()
Image1.Picture = LoadPicture("f:\tttt111.jpeg")
End Sub
Following is the url to download image
https://wetransfer.com/downloads/e2d4a91143507b3522bdd6632d69aa8b20161214051703/ae44f7
That is because you are using a PNG file and that is not supported by VB6. The supported formats are Bitmap, Icon, Cursor, Metafile, JPEG, and GIF. More info here...
Try converting the image to JPEG and it'll work.
We've had WIA 2.0 for a long time now. Here the database data is simulated by reading the file bytes:
Option Explicit
'
'Reference to: Microsoft Windows Image Acquisition Library v2.0
'
Private Sub Form_Load()
Dim F As Integer
Dim Bytes() As Byte
'Get data into Byte array:
F = FreeFile(0)
Open "oBSkm.png" For Binary Access Read As #F
ReDim Bytes(LOF(F) - 1)
Get #F, , Bytes
Close #F
With New WIA.Vector
.BinaryData = Bytes
Image1.Picture = .Picture
End With
With New WIA.ImageFile
.LoadFile "oBSkm.png"
Image2.Picture = .FileData.Picture
End With
End Sub
Preinstalled as part of Windows Vista and later. Was once available as an SDK with documentation and a redist version to be installed into Windows XP SP1 or later but no longer provided by Microsoft. You snooze, you lose.
I had same issue but I am happy I found working solution. The source of this issue for me was image dimensions was too large than image control I was trying to place image on, So All I had to do is change the image dimensions from 500 X 500 to 200 X 200 and error 481 in vba excel (invalid picture) was resolved.
Related
I am building a database and came across an issue that I need help in resolving. This database the customer wants to be able to link pictures to specific records. I have it so the pics are not OLE objects but links to a picture folder that will be on their network drive...So essentially the picture will be a hyperlink to the file path....
My question is does anyone know I way I can have the database reformat the picture automatically to 600 x 800 size, to help save space? We all know if I don't force the DB to do it for them it will not happen and potentially eat up valuable space, as this DB is expected to get quite large. So I would like to keep the picture folder as small as possible, giving the database more room.
You can use the WIA libary as shown in VBA – Resize Image like this:
Function ResizeImageTo600x800(ByVal PathToImage As String, ByVal PathToResizedImage As String) As Boolean
Dim WiaImgFile As Object 'WIA.ImageFile
Set WiaImgFile = CreateObject("WIA.ImageFile")
With CreateObject("WIA.ImageProcess") 'WIA.ImageProcess
.Filters.Add .FilterInfos("Scale").FilterID 'Add Scale Filter to ImageProcess
.Filters(1).Properties("MaximumWidth") = 600 ' Set Width to 600px
.Filters(1).Properties("MaximumHeight") = 800 'Set Height to 800px
'.Filters(1).Properties("PreserveAspectRatio") = False ' uncomment if AspectRatio should not be preseved
WiaImgFile.LoadFile PathToImage ' Load Image
.Apply(WiaImgFile).SaveFile PathToResizedImage ' Apply Filter and save resized Image
ResizeImageTo600x800 = True
Set WiaImgFile = Nothing
End With
End Function
Usage:
If ResizeImageTo600x800("\\path\to\image", "\\path\to\resized\image") then
Msgbox "ResizeImageTo600x800 successful!"
End If
Or:
ResizeImageTo600x800 "\\path\to\image", "\\path\to\resized\image"
Depending on your image types, you may also increase the compression of your image to save space. WIA should support this too (with the Convert Filter and its Quality Property).
I have looked far and wide and reached the end of my wits trying to figure out how to do this. I have looked on XtremeVBTalk.com and the rest of the internet on how to resize a damn StdPicture!
Does anyone know how to do this? Is this even possible?
Thank you so much in advance. I desire not to use any type libraries etc. so if that is offered in a solution I don't think I will be able to use it.
I'm not using A picturebox control at all.
Say I have the following function header, and an StdPicture is passed in:
Private Function EncodeImageToBase64(ByRef Image As StdPicture) As String
I then have the following declarations where I intend on encoding the StdPicture to base64:
Dim xmlDoc As DOMDocument60
Dim xmlNode As MSXML2.IXMLDOMElement
Dim bColor() As Byte
Dim bMask() As Byte
Dim bImage() As Byte
Dim lCrcTable() As Long
Dim lWidth As Long
Dim lHeight As Long
EncodeImageToBase64 = vbNullString
If Image Is Nothing Then
Exit Function
End If
Call CRCTable(lCrcTable)
Call Icon2Arrays(Image, bColor, bMask, lWidth, lHeight)
If Not CreatePngByteArray(bImage, lWidth, lHeight, bColor, bMask, lCrcTable) Then
Debug.Assert False
Exit Function
End If
However, before calling that, I want to cut the image's width and height in half. How can I do so? CreatePngByteArray only supports 16x16 PNGs and I am using 32x32, so I'd like to cut them down in order to pass the asserts they have.
OK, I spent quite some time that I didn't really have on this one, because I didn't know the answer to begin with, but I was still interested in finding out what the potential solution was.
The following answer is my understanding of what you are trying to do, but may not be the answer to the question itself, so it could very well be considered wrong.
So, here's what I came up with. You will need to use an IPictureDisp object instead of an StdPicture object. You will also need to use a PictureBox control, even if you don't really want to.
Create a new project. Add a form, or open an existing one if one is provided. Set the ScaleMode of the form to pixels. Add a PictureBox control on the form. Set the AutoRedraw property of the PictureBox control to 'True', the BorderStyle property of the control to 'None', and the Height and Width properties of the control to 16 pixels each. Add the following code to the form, and modify the location and type of the image that you want to resize, and then the location to save it to:
Private Sub Form_Load()
Dim TestPic As IPictureDisp
Set TestPic = LoadPicture("C:\Users\Your Name\Desktop\image.gif")
With TestPic
.Render Picture1.hDC, 0, 16, 16, -17, 0, 0, .width, .height, 0
End With
SavePicture Picture1.Image, "C:\Users\Your Name\Desktop\image2.bmp"
End Sub
The image can start with any of the types that Visual Basic 6 supports (.bmp, .cur, .gif, .ico, .jpeg or .jpg, and .wmf), but must always be saved in bitmap format. Please note that Visual Basic 6 does not support PNG file formats at all, so you will not be able to use any VB6 functions to open PNG files or create them.
I would be interested in other solutions that other people come up with.
Edit: Fixed dimensions.
Do you mean inside the PICTUREBOX control or inside an IMAGE CONTROL? Because If I remember correctly it has a STRETCH property which autofits the image to the container
#Zaf Khan beat me to it. I have something similar where I have a PictureBox. Under behaviour I have SizeMode set to StretchImage then when I load an image like so
LoadWebImageToPictureBox(ImagePreview, SelectedFile)
it auto fits.
I'm working with Access 2010's new ribbon feature, and I'm trying to figure out the simplest way to load an image to the ribbon from a table. I know that I could do this by using a GDI API function, but from what I can see the code looks convoluted and appears to utilize extra dlls that I would just as well avoid calling if I don't need to. I also know that I could create a control on a form, load the image to the form, and then load the image from the form to the ribbon image variable. While using the form is doable, it seems sloppy. Eventually, I will resort to one of the two preceding methods if I have to, but it just seems like there should be some simple way to load an image from a table directly into an image variable for the ribbon.
One thing to note is that I'm willing to use whatever image format is easiest. These icon files are very small and so I'm not concerned about file size and I also don't care whether or not they are capable of transparency. So if there's a method that only works with bitmap file types that is fine with me.
Here's the code I have for the button in the ribbon XML:
<button id="CCTrans" label="Credit Card Trans." getImage="GetMnuIcn" onAction="=OpenCCTrans()" />
And here's the code I have for retrieving the image:
Sub GetMnuIcn(control As IRibbonControl, ByRef Image)
Set Image = DLookup("Img", "LtblImg", "Lbl=""" & control.ID & """")
End Sub
I've tried storing the image both as an OLE Object and as an attachment. Either way, when I try to retrieve it, it gives me the error "Type Mismatch". For simplicity, I've been storing it as a bitmap, but I also tried .gif and .png in my first attempts.
Note: Apparently the ribbon icons are IPictureDisp objects. If I can just figure out how to load an image from a table into one of those object types, I should be able to use it for the ribbon.
Also, there's a lot of good information on Microsoft's website, but I haven't been able to piece it together into a solution yet. Here's a link: http://msdn.microsoft.com/en-us/library/bb187398.aspx
Well after tedious days of trying to find a better solution, I'm forced to pick between what I feel are various mediocre approaches. I really wanted to just load it directly into a variable, but the only method for doing that requires references to a bunch of dlls and has code that is pretty complicated. To avoid any extra dependencies and to keep my code simpler, I decided to go the route of assigning the table containing the attached images to a form, opening that form in hidden mode and then assigning the picture variables from the form.
For some reason the code that I saw elsewhere that loaded the pictures on demand via this method caused my program to crash and so I had to load the pictures into variables when the program opened and then have the menu bar set the icons from those variables when the menu is selected.
My code is as follows:
Dim Icn(15) As IPictureDisp, IcnLbl(15) As String
Function SetMnuIcn()
Dim Img As Recordset, i As Byte
DoCmd.OpenForm "frmImg", acNormal, , , acFormReadOnly, acHidden
Set Img = Forms!frmImg.Recordset
While Not Img.EOF
i = i + 1
Set Icn(i) = Forms!frmImg!Img.PictureDisp
IcnLbl(i) = Img!Lbl
Img.MoveNext
Wend
DoCmd.Close acForm, "frmImg", acSaveNo
End Function
Sub GetMnuIcn(control As IRibbonControl, ByRef Image)
Set Image = Icn(GetAryLoc(IcnLbl, control.ID, 15))
End Sub
Function GetAryLoc(varValues As Variant, varMatch As Variant, NumOfValues As Integer) As Integer
Dim i As Integer
For i = 1 To NumOfValues
If varValues(i) = varMatch Then GetAryLoc = i
Next
End Function
Note: To ensure that the icons have been loaded prior to when they are needed, I specified SetMnuIcn to run on ribbon load:
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="=SetMnuIcn()">
I am building an Excel 2010 workbook that will use images to represent boards plugging into slots. Currently, I am using LoadPicture to pull the image from the disk to use on the worksheet. But when I send the workbook to somebody, they get error messages unless I send the image files also. Is there a way to actually have the image files be a part of the workbook file so that I don't have to package the image files with the workbook?
Thank you for your help!
When you get the picture, make sure the picture is saved with the document and not just a link:
Sub GetThePicture()
ActiveSheet.Shapes.AddPicture _
"c:\TestFolder\sample.jpg", _
msoFalse, msoTrue, 100, 100, 70, 70
End Sub
I have some images and videos in my powerpoint presentation. Depends of what format have each one (jpg, mp3, jpeg,...) I want it to make a different thing so I need to know what kind of file is it. Is there anything in Visual Basic to make it?
EDITED
I want to difference the images and videos supported by iPad with the ones that are not supported. That is why I need to know the format of them.
EDIT: to state it clear- solution below works only with shapes which are linked to files!
Function WhatTypeOfMedia(SHP As Shape)
Select Case Right(SHP.LinkFormat.SourceFullName, 4)
Case ".jpg", "jpeg"
WhatTypeOfMedia = "Picture type"
Case ".wav", ".mp3"
WhatTypeOfMedia = "Music type"
'... end other possible types put here
End Select
End Function
To make a test run for 1st slide of active presentation try this code:
Sub Take_format_Linked()
On Error Resume Next 'required for these files which are not linked
Dim SHP As Shape
For Each SHP In ActivePresentation.Slides(1).Shapes
'1st check if format is linked- best option:
If Len(SHP.LinkFormat.SourceFullName) > 0 Then
If Err.Number = 0 Then
'if so, let's call function
Debug.Print WhatTypeOfMedia(SHP)
'here you can do what you want based on function result
Else
Err.Clear
End If
End If
Next
End Sub
As a result you get information in Immediate window which shape is which type. You could use that types to run what you need in main sub.
In that simple idea I kept On Error Resume Next to avoid error if Shape is not linked.
As has been pointed out, you can get the file types for linked files, but there's no way to get them for embedded files. However, if you don't need to do it in VBA or can manipulate ZIP files in VBA, open the PPTX as a zip and look for the media folder. There you'll find any embedded files (pictures, sounds, movies).
They may not be in the same format as they originally were in; PPT sometimes converts images when it imports them. But that shouldn't be an issue considering what you're after.